javascript PHP 和 Ajax 文件上传 - 无法使用 $_FILES 获取 tmp_name

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13033104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:40:43  来源:igfitidea点击:

PHP and Ajax file upload - Can't get the tmp_name with $_FILES

phpjavascriptajaxfile-upload

提问by don

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:

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

JS:

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:

PHP:

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

It works fine and xmlhttp.responseTextprints [object File].

它工作正常并xmlhttp.responseText打印[object File]

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:

但是,我的问题是我需要使用$_FILES["q"]['tmp_name']. 为此,我已将代码更改为以下内容:

JS:

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:

PHP:

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

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

问题是现在xmlhttp.responseText我什么也没得到。有谁知道我做错了什么?

采纳答案by phpisuber01

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

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

jQuery Upload Progress and AJAX file upload

jQuery 上传进度和 AJAX 文件上传

--

——

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.

或者,如果您想即时上传,有一个很酷的库,您可以将其称为“Uploadify”。它是一个 flash/jquery(或现在的 HTML5)装备,允许您即时上传文件。在 flash 版本中,我上次使用它时……您可以添加回调函数,使其基本上可以做任何您想做的事情。

Some clever javascript could make this work for you.

一些聪明的 javascript 可以为您完成这项工作。

http://www.uploadify.com/

http://www.uploadify.com/

回答by Marc B

AJAX doesn't do file uploads. It's not designed for that. The standard workaround is to have the JS code build a hidden iframe and do a standard POST-type upload in that. As such, if you try doing echo $_FILES['q']['error'], you'd probably have gotten 4for "no file".

AJAX 不进行文件上传。它不是为此而设计的。标准的解决方法是让 JS 代码构建一个隐藏的 iframe 并在其中执行标准的 POST 类型上传。因此,如果您尝试这样做echo $_FILES['q']['error'],您可能会得到4“无文件”。