This question has already been answered:

I can not figure out how to upload 2 or more images to the server.

<script> $("#btnAddAd").click(function () { var files = $('#input-ficons-1').fileinput('getFileStack'); var description = $('#description').val(); var formData = new FormData(); formData.append('description', description); for (var i = 0; i < files.length; i++) { formData.append('file', files); } $.ajax({ type: 'POST', data: formData, url: 'ajax/uploaded-image.php', processData: false, contentType: false, multiple: true, success: function (data) { alert(data); } }); }) </script> 

PHP: echo '<pre>'; print_r($_FILES['file']); echo '</pre>'; echo '<pre>'; print_r($_FILES['file']); echo '</pre>';

Displays:

 <pre>Array ( [name] => CauSYrOVs7k.jpg [type] => image/jpeg [tmp_name] => Z:\tmp\php6923.tmp [error] => 0 [size] => 146070 ) </pre> 

I can not understand how to go through the array of images in PHP, with JS Ajax, it seems like it transfers all the files.

Reported as a duplicate by participants Alexey Shimansky , Community Spirit 30 May '16 at 1:20 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

Found a solution:

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

PHP:

 echo $_FILES['files']['name'][0]; echo "\n<br>\n"; echo $_FILES['files']['name'][1];