简体   繁体   中英

cannot get multiple uploaded files from $_FILES in 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.

(PS 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 )

Your name should be array:

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

This code doesn't implement your goals. You should use several input tags.

If you want to upload several files but number of files is variable you can use java script for getting number of files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM