Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot get multiple uploaded files from $_FILES in php

Tags:

html

php

I cannot retrieve multiple files in my php code from $_FILES. Here is the input form:

<form enctype="multipart/form-data" action="file-upload.php" method="POST">
  Upload the several files:<input type="file" multiple="multiple" name="uploaded" id="id_upload" />
  <input type="submit" value="Upload" />
</form>

Here is the php code from file-upload.php:

 // first let's find out how many files were uploaded..
 $numUploadedfiles = count($_FILES['uploaded']);
 $num_FILES = count($_FILES); 
        // BOTH COUNTS ARE 5.  I SELECT 7 FILE NAMES FOR UPLOADING THOUGH.


 echo "<br>" . "The number of uploaded files is == " . $numUploadedfiles;
 echo "<br>" . "Here is the name of _FILES['uploaded']: " . $_FILES['uploaded'];
     // THE NAME REPORTED IS 'array' AND THE COUNT IS 5..


 echo "<br>" . "The count size of _FILES is == " . $num_FILES;
 echo "<br>" . "Here is the name of _FILES => " . $_FILES;
       // HERE ALSO, THE NAME REPORTED IS 'array' AND THE COUNT IS 5.


 echo "<br>file temp_name " . $i . " is: " . $_FILES['uploaded']['tmp_name'];
 echo "<br>file name " . $i . " is: " . $_FILES['uploaded']['name'];
        // THE NAME REPORTED HERE IS THE FILENAME OF LAST OF THE 7 FILES I UPLOADED (not sure why.)


 echo "<br>" . "Here are the filenames: ";
 for($i = 0; $i < $numUploadedfiles; $i++)
 {
    echo "<br>filename " . $i . " is: " . $_FILES['uploaded'][$i];
 }
 exit();

What happens when I run this is, when the 'for' loop starts, an error message saying that the $i indexes into the array _FILES['uploaded'][$i] are not valid.

Why is that? I need to get these 7 file names and be able to save them on the server. How can I:

1) get an accurate 'count' of the number of files? The code above give a count of 5 when I'm uploading 7 files

2) how do I correctly index through the _FILES array in a 'for' loop? PHP is telling me the $i values of 0, 1, 2, 3.... are not valid.

(P.S. I am using the input type="file" multiple="multiple" name="uploaded" id="id_upload" code from the example I saw for enabling multiple file uploads at Retrieving file names out of a multi-file upload control with javascript)

like image 205
SandHawkerTech Avatar asked May 06 '26 23:05

SandHawkerTech


1 Answers

Your name should be array:

<input type="file" name="uploaded[]" id="id_upload" />

like image 176
Sudhir Bastakoti Avatar answered May 08 '26 13:05

Sudhir Bastakoti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!