1

I've have been coding files uploads via jQuery AJAX. Everything worked fine unitl I was trying to upload files approximately greater 4 MB. What am I missing?

Fact are following:

  1. php.ini - checked (max upload file size, max execution time etc.)
  2. server-side script which handles files uploads is executed but $_POST and $_FILES are empty
  3. var fd seems to process correctly

Chunk of jQuery:

var x_upload = $(".x_upload");

x_upload.on('drop', function(e) {
    e.preventDefault();
    var files = e.originalEvent.dataTransfer.files;
    handleFileUpload(files, x_upload);
});

function handleFileUpload(files, x_area) {
    for (var i = 0; i < files.length; i++)
    {
        var fd = new FormData();
        fd.append('file', files[i]);
        console.log(files[i]); //is OK
        fd.appdend('type', 'image');
        sendFileToServer(fd, x_area);
    }
}

function sendFileToServer(formData, x_area) {
    var uploadURL = "/api/upload.php";
    var jqXHR = $.ajax({
        xhr: function() {
            var xhrobj = $.ajaxSettings.xhr();
            if (xhrobj.upload) {
                xhrobj.upload.addEventListener('progress', function(event) {
                    var percent = 0;
                    var position = event.loaded || event.position;
                    var total = event.total;
                    if (event.lengthComputable) {
                        percent = Math.ceil(position / total * 100);
                    }
                    //Set progress
                    x_area.find(".x_progress_num").text(percent);
                }, false);
            }
            return xhrobj;
        },
        url: uploadURL,
        type: "POST",
        contentType: false,
        processData: false,
        cache: false,
        data: formData,
        success: function(data) {
            console.log(data);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            console.log("AJAX Error " + errorThrown + " " + textStatus);
            x_area.addClass('x_error');
        }
    });
}

Simplified server-side

<?php

var_dump($_POST); // is empty if file is >4 MB
var_dump($_FILES); // is empty if file is >4 MB

if (!isset($_POST['type'])) {
    echo "empty type";
} else {
    //echo "ok' only if file is ls <4MB
    echo "ok";
}

So might be there any file size limit which I've overlooked? Thank you.

DaveB
  • 2,953
  • 7
  • 38
  • 60
Daniel.P.
  • 480
  • 2
  • 8
  • 19
  • 1
    Which php.ini settings did you check, exactly? [PHP change the maximum upload file size](http://stackoverflow.com/questions/2184513/php-change-the-maximum-upload-file-size) – Jonathan Lonowski Apr 30 '14 at 22:20
  • If `upload_max_filesize` was to blame, then you should get an `error` entry in `$_FILES`. Have you checked what data gets _send_ by the browser in the first place (developer tools)? – CBroe Apr 30 '14 at 22:27
  • @JonathanLonowski has right. I have upload_max_filesize set to 200M but I forgot to increase post_max_size. I feel silly now... – Daniel.P. Apr 30 '14 at 22:32

1 Answers1

0

I had upload_max_filesize = 200M but I forgot to increase post_max_size (as @Jonathan Lonowski pointed out). This solved my problem.

However, I'am surprised that $_FILES["file"]["error"] > 0 returned FALSE as well as $_POST returned an empty array (although the value was assigned statically).

Thank you. Mystery solved.

Daniel.P.
  • 480
  • 2
  • 8
  • 19
  • 2
    _“However, I'am surprised […]”_ – that is defined behavior: [“If the size of post data is greater than post_max_size, the $_POST and $_FILES superglobals are empty.”](http://www.php.net/manual/en/ini.core.php#ini.post-max-size) – which makes sense, because giving your script only _parts_ of the data to work with can of course not be the alternative, that would lead to all kinds of trouble. – CBroe Apr 30 '14 at 22:52