PHP和Ajax文件上传-无法通过$ _FILES获得tmp_name [英] PHP and Ajax file upload - Can't get the tmp_name with $_FILES

查看:143
本文介绍了PHP和Ajax文件上传-无法通过$ _FILES获得tmp_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Ajax上传文件,但是在处理文件时遇到了麻烦...为了进行测试,我构建了一个简单的代码,如下所示:

I'm trying to upload a file using Ajax, but I'm having troubles handling the file... For test purposes I've build a simple code that looks like this:

JS:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);

document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;

PHP:

$q=$_POST["q"];
echo $q;

它工作正常,并且 xmlhttp.responseText 打印 [目标文件] .

It works fine and xmlhttp.responseText prints [object File].

但是,我的问题是,我需要使用 $ _ FILES ["q"] ['tmp_name'] 获得临时文件名.为此,我将代码更改为以下内容:

My problem, however, is that I need to get the temporary file name with $_FILES["q"]['tmp_name']. To do so I have changed the code to the following:

JS:

xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("enctype","multipart/form-data");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);

document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;

PHP:

$q=$_FILES["q"]["tmp_name"];
echo $q;

问题是,现在使用 xmlhttp.responseText 我什么都没得到.有人知道我在做什么错吗?

Problem is that now with xmlhttp.responseText I don't get anything. Anyone knows what I'm doing wrong?

推荐答案

查看此答案以使用AJAX上传文件.可能,但并非在所有浏览器中都兼容.

Check out this answer for making file uploads with AJAX. It is possible, but not compatible in all browsers.

jQuery上传进度和AJAX文件上传

-

或者,如果您想即时上传,则可以使用一个很酷的库,名为"Uploadify".这是一个Flash/jquery(或现在的HTML5)装备,可让您即时上传文件.在Flash版本中,我上次使用它是...您可以添加回调函数,使其基本上可以执行您想要的任何事情.

Alternatively, if you want on the fly uploads, there is a cool library you can get called 'Uploadify'. It's a flash/jquery (or HTML5 now) rig that allows you to upload files on the fly. In the flash version, last time I used it... you can add in callback functions to make it do essentially anything you want.

一些聪明的javascript可以帮助您完成这项工作.

Some clever javascript could make this work for you.

http://www.uploadify.com/

这篇关于PHP和Ajax文件上传-无法通过$ _FILES获得tmp_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆