S
S
Soft_touch_plastic2021-02-22 16:48:08
AJAX
Soft_touch_plastic, 2021-02-22 16:48:08

Why does $_FILES contain only the last file sent by the array?

Hello, there is a form in which you can select multiple files (multiple="true"). I write all the files to the FormData object and send it to the server, but there, in $_FILES, there is only the last file for some reason.
html:

<input type="file" name="file" id="file-input" multiple="true">

js:
var fd = new FormData();    
                for (var i = 0; i < input.files.length; i++) {
                    fd.append('file', input.files[i]);
                }

                $.ajax({
                    url: 'server.php',
                    data: fd,
                    processData: false,
                    contentType: false,
                    type: 'POST',
                    success: function(data){
                        console.log(data);
                    }
                });


php:
var_dump($_FILES);
The latter only returns:
array(1) {
  ["file"]=>
  array(5) {
    ["name"]=>
    string(14) "myypdf (5).pdf"
    ["type"]=>
    string(15) "application/pdf"
    ["tmp_name"]=>
    string(56) "some\php_upload\php6133.tmp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(428652)
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
no_one_safe, 2021-02-22
@Soft_touch_plastic

for (var i = 0; i < input.files.length; i++) {
                    fd.append('file['+i+']', input.files[i]);
                }

A
Anton, 2021-02-22
Semenov

name="file" and you need name="file[]"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question