Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
Uploading files by AJAX and geting error in PHP handling

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 ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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 !

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!