2

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!

Stephan
  • 594
  • 7
  • 17
  • Could you show the request headers too? – CBroe May 16 '14 at 23:14
  • @croe in question i shared this link : https://www.dropbox.com/s/tfta4ulqlxsaism/csz.PNG – Stephan May 16 '14 at 23:16
  • _Yes_ … and there we can not see the _request headers_, because you did not _unfold_ them. – CBroe May 16 '14 at 23:17
  • Oh yes , https://www.dropbox.com/s/tinat2h39rq7tvq/sxc.PNG – Stephan May 16 '14 at 23:20
  • @cbroe i tried but still empty with a Warning: Missing boundary in multipart/form-data POST data – Stephan May 16 '14 at 23:26
  • Did you specify the actual boundary in the Content-Type header value as well? – CBroe May 16 '14 at 23:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/53856/discussion-between-mostafa-and-cbroe) – Stephan May 16 '14 at 23:31
  • Sorry, according to http://stackoverflow.com/a/5976031/1427878 setting `contentType: false` in your code seems to be correct … although that should not mean that the _actual_ header in the send request should have that value … not sure why that is not working correctly. – CBroe May 16 '14 at 23:47

2 Answers2

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.

man
  • 498
  • 5
  • 12
-1

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,
volkinc
  • 2,143
  • 1
  • 15
  • 19