php - Sending multiple files using ajax without using html form -


im using single file input field multiple upload property. i've tested single file pass , worked. i'm trying pass files using array there mistake. there no form.

html:

<input id="fileinfo" name="userfile[]" type="file" multiple> 

js:

var formdata = new formdata(); var files = []; for(var = 0; < length; i++) {     files[i] = $('input', '#fileinfo')[0].files[i]; } formdata.append('userfile', files); $.ajax({     url: "example.php",     data: formdata,     type: 'post',     datatype: 'json',     processdata: false,     contenttype: false,     success: function(res)     {         console.log("done");     } }); 

php:

<?php $length = sizeof($_files['userfile']['name']); json_encode(array($length)); 

error.log:

php notice:  undefined index: userfile in /path/to/php on line 2, referer: http://localhost/test 

instead of building array files (which kind of strange since source array), append files directly formdata object:

var formdata = new formdata();  // files can length etc. // not necessary, make code easier read. var files = $('input', '#fileinfo')[0].files;  for(var = 0; < files.length; i++) {     formdata.append('userfile[]', files[i]); }  $.ajax({     // ajax call }); 

we've changed 'userfile' 'userfile[]' make array can loop through in php.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -