Search code examples
phpajaxmultipartform-dataform-data

Formdata send only the name of the first object


I try to upload files asynchronously but I've got a problem I cannot solve. I have an input field without form element, it contains raw data and files. When I try to upload, the very first formdata file object fails I guess on server side. the $_FILES variable contains only the name of the first file but after the first everything works fine. Html:

<input class="form-control fileInput" id="applInputIdentityFile" type="file" accept=".pdf,.jpg,.png,.gif" style="display: none">

JS:

$('.fileInput').on('change',function(){
    applToSend.arrayToSend = new FormData();
    $('.fileInput').each(function(){
      var fileName = $(this).val().replace(/^.*\\/, "");
      if(fileName){
        $("#" + $(this).attr("id") + "Name").html(fileName);
        $(this)[0].files[0].descr = $(this).attr("id");
        applToSend.arrayToSend.append($(this).attr("id"), $(this)[0].files[0]);
      }
    });
  });

The frontend object contains the right values, and the ajax header seems fine for me:

------WebKitFormBoundaryDcqJbGjmdx2dg3q6
Content-Disposition: form-data; name="applInputIdentityFile"; filename="20200430_115117.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryDcqJbGjmdx2dg3q6
Content-Disposition: form-data; name="applInputProdCertFile"; filename="20200430_115117_resized.jpg"
Content-Type: image/jpeg

the Ajax itself:

    $.ajax({
      url: '/application/addAppl',
      type: 'POST',
      data: applToSend.arrayToSend,
      timeout: 30000,
      processData: false,
      contentType: false,
      cache: false,
      enctype: 'multipart/form-data',
    })

but the very first file always fails, doesn't matter how many files I try to upload or which input, which file, which extension I use. On server side there is only these lines, everything else has been commented out (I use MVC, so it's the model of the request after several hundreds line of code):

  public function addAppl($data){
    foreach($_FILES as $key=>$value){
      foreach($value as $kkey=>$vvalue){
        echo "<br>".$key.": ".$kkey." :".$vvalue;
      }
    }
  }
applInputIdentityFile: name :20200430_115117.jpg
applInputIdentityFile: type :
applInputIdentityFile: tmp_name :
applInputIdentityFile: error :1
applInputIdentityFile: size :0
applInputProdCertFile: name :20200430_115117_resized.jpg
applInputProdCertFile: type :image/jpeg
applInputProdCertFile: tmp_name :/tmp/phpLoBwTl
applInputProdCertFile: error :0
applInputProdCertFile: size :382022

Could you help me how to solve this?


Solution

  • You have to edit the upload_max_filesize

    You can edit the htaccess file, add this line

    php_value upload_max_filesize 256M //256M more or less
    

    Or edit the php.ini file, locate this and change it:

    upload_max_filesize = 256M //256M more or less