I'm banging my head on the walls with this thing for 4 hours now.
I've created an input (files multiple) as show below, that is not inside a form.
<label for="adaugaFisiere" class="btn btn-success tip" title="Poți adăuga fișiere JPG,PDF,DOC,XLS" data-original-title="Adauga imagini"><span>Adaugă fișiere</span></label>
<input type="file" multiple="" id="adaugaFisiere" name="docImgs[]" class="filestyle invisible" required="" accept="image/*,.pdf,.doc,.docx,.xls,.xlsx" onChange="updateList(<?= $nextNrDoc ?>)">
Next , I have the following JavaScript code, that uploads the files to an PHP file
function updateList(nrDoc){
var inputFiles = $('#adaugaFisiere').prop('files');
var formData = new FormData();
formData.append('listFiles', inputFiles);
$.ajax({
url: 'exec/files-upload.php',
type: 'POST',
cache: false,
data: formData,
contentType: false,
processData: false,
//Ajax events
success: function(html){
$('#fileList').html(html);
},
fail: function(res){
alert('Eroare upload');
}
});
}
And finally, the PHP file
var_dump($_POST);
$name_array = $_FILES['listFiles']['name'];
$tmp_name_array = $_FILES['listFiles']['tmp_name'];
// Number of files
$count_tmp_name_array = count($tmp_name_array);
The PHP file is bigger, but it's not important, because I get an error at the top of the file.
The dump_var returns this:
array(1) { ["listFiles"]=> string(17) "[object FileList]" }
And right after comes the error:
Warning: Undefined array key "listFiles" in D:\xampp\htdocs\cityadmin\exec\files-upload.php on line 4
Warning: Trying to access array offset on value of type null in D:\xampp\htdocs\cityadmin\exec\files-upload.php on line 4
Warning: Undefined array key "listFiles" in D:\xampp\htdocs\cityadmin\exec\files-upload.php on line 5
Warning: Trying to access array offset on value of type null in D:\xampp\htdocs\cityadmin\exec\files-upload.php on line 5
I ran in circles but couldn't figure it out. Why I get this error, if dump_var shows the data arriving at the PHP file ? Anyone, any ideeas ?
Please don't vote on this, anyone, I've just posted for those who are in the same situation as I am.
I got my answer from this thread
PHP/Javascript/AJAX file upload via drag and drop. Files get converted to strings
The point is when you have multiple files from an input, you need to loop through each one and add it to the formData as an array. After that, you can retrieve it in PHP as $_FILES[][].
It is possible to append another $_POST[] data to the formData, with the formData.append() and get both a $_POST[] and a $_FILES[][] array in PHP handler.
So, instead of doing this formData.append("fileList", fileList);, I've done this
for (let i = 0; i < fileList.length; i++) {
formData.append("fileList[]", fileList[i]);
}
formData.append('nextId', nrDoc);
formData.append('post', 'files-upload');
After that change, I could access the files in PHP handler, and my other variables sent along.
It used to work some time ago, but I've been busy with other type of projects and I didn't realize that something has definitely changed in the way AJAX process requests.
Now, everything work as designed, without any warnings or errors. Hope somebody else will find a solution in this post !