Ajax上传文件:$ _FILES为空,但文件存在于请求标头中 [英] Ajax Upload File: $_FILES is empty but files exists in request header
问题描述
当用户在页面上删除文件时,我使用formdata上传文件。在客户端一切正常,文件详细信息存在于Request标头中,但是当我打印__($ _ FILES)时,它返回一个空数组。没有设置服务器端限制。我通过手动上传文件来测试它。
I am using formdata to upload file when user drops files on page. everything works fine in client side and file details exist in Request header but when i print_r($_FILES), it returns an empty array. No Server side limit is set. i did test it by uploading a file manually.
请求详细信息: https://www.dropbox.com/s/tfta4ulqlxsaism/csz.PNG
js代码:
$('html').live('drop', function(e)
{
try
{
e.stopPropagation();
e.preventDefault();
var files = e.originalEvent.dataTransfer.files || e.target.file || e.dataTransfer.files;
var file;
var len = files.length;
var i =0;
var formdata = new FormData();
for ( ; i < len; i++ ) {
file = files[i];
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
$('html').removeClass('hover');
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("files[]", file);
}
}
if (formdata)
{
$.ajax({
url: base_url+"/kh/site/file/upld",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success : function(res){
console.log(res);
},
error: function(res){
console.log(res);
}
});
}
return false;
}catch(a){console.log(a.message);}
});
PHP代码:
<?php print_r($_FILES); ?>
我缺少什么?
谢谢提前!
推荐答案
我遇到了完全相同的问题,结果证明这不是AJAX错误。说到PHP,上传不仅限于 upload_max_filesize
。在服务器上配置上传参数时,请考虑以下内容:
I had the exact same issue, and it turns out it is not an AJAX fault. When it comes to PHP, upload is limited NOT only by upload_max_filesize
. Take into consideration the following ini's when configuring upload params on your server:
max_execution_time
:如果你设置 upload_max_filesize
到100mb,脚本的max_execution_time为5秒,服务器最有可能返回404或500.
max_execution_time
: if u set upload_max_filesize
to 100mb and the script has a max_execution_time of 5 sec., the server will most likely return 404 or 500.
max_input_time
:您希望此值足够高,以便完成完整上传,具体取决于 upload_max_filesize
。如今的互联网连接有很大的下载带宽,但上传是有问题的。
max_input_time
: u want this to be high enough in order for a full upload to complete, depending on upload_max_filesize
. Internet connections nowadays have a large download bandwidth, but the upload is questionable.
post_max_size
:上传过程是由定义,一个$ _POST请求,所以如果 upload_max_filesize
的值为100mb,你就不能有 post_max_size
的值那个更低。嗯,实际上你可以,但上传时,如果总上传大小介于 post_max_size
和 upload_max_filesize
之间,$ _FILES将会仍然是空的。
post_max_size
: an upload process is by definition, a $_POST request, so if upload_max_filesize
has a value of 100mb, u can't have a post_max_size
value that is lower. Well, actually u can, but when uploading, if the total upload size falls between post_max_size
and upload_max_filesize
, $_FILES will still be empty.
这篇关于Ajax上传文件:$ _FILES为空,但文件存在于请求标头中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!