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
Post a Comment