Multiple file upload by AJAX and problem with post_max_size

1

Good afternoon! I am creating a multiple file upload with jQuery / AJAX and PHP. Doing tests, I've noticed that when I select a total of + 3mb of files, it does not upload me. When it's less than that, it does.

I have looked at my phpinfo and I have seen that the parameter upload_max_filesize is 64M, but looking more, I have seen that the parameter post_max_size is 3M. I think that's the problem, that when uploading it with POST my server compares with post_max_size instead of upload_max_filesize.

If I do not want to change the post_max_size parameter ... how can I make the AJAX request to take it as a file upload instead of a POST?

function subir_adjuntos_licencia(){
    //Creamos un objeto formData
    var fd = new FormData();
    //fd.append("estado", $("#selestados option:selected").val());
    for (var i=0, len=document.getElementById('adjuntos_licencia').files.length; i< len; i++) 
    {
        fd.append("adjuntos_licencia"+i , document.getElementById('adjuntos_licencia').files[i]);
    }
    //Petición AJAX           
    $.ajax({
        url : "<?= site_url('admin/licencias/detalle_licencia_usuario/subir_adjuntos_licencia_usuario?ins='.$institucion_id.'&lic='.$id_licencia);?>",
        type: "POST",
        data : fd,
        processData: false,
        contentType: false,
        success:function(data, textStatus, jqXHR){
            if(data.error == false)
            {
                $("#adjuntos_licencia").fileinput('clear');
                toastr.success(data.mensaje);
            }
            else
            {
                 toastr.error(data.mensaje);
            }

        },
        error: function(jqXHR, textStatus, errorThrown){
             toastr.error(textStatus);
        }
    });
}
    
asked by Juan Luis 25.01.2018 в 13:07
source

1 answer

3
  

How can I make the AJAX request to take it as a file upload instead of a POST?

I think you're confusing, one thing is not related to the other.

POST is simply one of the methods defined for HTTP communications. It is basically a "key" to indicate that the message will modify data on the server.

That the content of the message is the data of a form, or attachments (files), or anything else, does not affect whether the message is POST or not. Neither that the request is Ajax or not, that the client is a browser or not, etc ...

  

I think that's the problem, that when uploading it with POST my server compares with post_max_size instead of upload_max_filesize.

I should compare it to both. The post_max_size verifies the size of the HTTP message, the upload_max_filesize the size of the different files.

To put an analogy, it is as if you send items in a package by mail. At the post office they will check if the package you send meets the valid limits for the package, once the package arrives at the destination and the recipient opens it will check if what is inside the package is worth it.

I see three alternatives:

  • Modify post_file_size only for those URLs. Unfortunately according to this question it seems that you should put the value maximum for the whole site and, where you want a lower value, do the checks in PHP.

  • Using PUT: You'll have to configure the server . And you'll have to assemble you the message (nothing about the browser separating the different files, putting the values of the parameters, etc.) and processing it; I recommend using a different call for each file.

  • Remove communication from HTTP files and use a WebSocket. It seems complicated, and I do not think it's worth it.

answered by 25.01.2018 / 13:21
source