Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top