问题
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.
Request Details : https://www.dropbox.com/s/tfta4ulqlxsaism/csz.PNG
js Code :
$('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 Code :
<?php print_r($_FILES); ?>
What i am missing?
Thanks in advance!
回答1:
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
: 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
: 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
: 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.
回答2:
2 reasons you need append 1 file file at once
formData.append( 'fileName', fileData);
fileName[] -- will not work
Add cash:false
$.ajax({
type: "POST",
url: url,
data:formData,
**cache: false,**
contentType: false,
processData: false
also you can have wrong congiguration of dropZone Dropzone.options.myAwesomeDropzone = {
url: "post_ad.php",
method: "post",
**enctype:"multipart/form-data",**
paramName: "files",
addRemoveLinks: true,
autoProcessQueue: false,
**uploadMultiple: true,**
parallelUploads: 100,
来源:https://stackoverflow.com/questions/23705796/ajax-upload-file-files-is-empty-but-files-exists-in-request-header