Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda