• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

Multiple file uploads in form - how to loop over $_FILES?

Hi

I have a form with several inputs.
<input name="file1" type="file" ...>
<input name="file2" type="file" ...> etc

When I handle the uploads in PHP, I want to be able to loop over all the $_FILES[] for each file.
I tried this
for($i = 1; $i <= $no_upload_files; $i++)
      $file_no = sprintf("file%d", $i);
      $fileName = $_FILES[$file_no]['name'];
      $tmpName  = $_FILES[$file_no]['tmp_name'];
etc

but it doesn't work.

What am I doing wrong? Is it possible even?

Thanks
tr5
0
tr5
Asked:
tr5
  • 2
  • 2
1 Solution
 
gamebitsCommented:
use an array rename your input name="file[]"

and then to process the file.

while(list($key,$value) = each($_FILES[images][name]))
            {
                  if(!empty($value))
                  {
                        $filename = $value;
                              $add = "upimg/$filename";
                       //echo $_FILES[images][type][$key];
                       // echo "<br>";
                              copy($_FILES[images][tmp_name][$key], $add);
                              chmod("$add",0777);

                                                echo "&nbsp;&nbsp;&nbsp;<img src=\"http://www.yourdomain.com/$add\">";


                  }
            }
0
 
tr5Author Commented:
I don't understand. Can you write the actual code for this please
>use an array rename your input name="file[]"

Also, what would my equivalent of your "images" be ?
0
 
gamebitsCommented:
Sorry, I used the script to upload images.

Here is a modified version to upload file, the first part create a dynamic form with multiple upload ( set to 2 in the script ).

<?
$max_no_file=2;  // You can set how many form fields you will have here.
echo "<form method=post action=uploadfile.php enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for($i=1; $i<=$max_no_file; $i++){
echo "<tr><td>File $i</td><td>
      <input type=file name='file[]'></td></tr>";
}
echo "<tr><td colspan=2 align=center><input type=submit value='Upload File(s)'></td></tr>";

echo "</form> </table>";

?>


###This is the uploadfile.php###
<?

while(list($key,$value) = each($_FILES[file][name]))
            {
                  if(!empty($value))
                  {
                        $filename = $value;
                              $add = "upimg/$filename";
                       //echo $_FILES[file][type][$key];
                       // echo "<br>";
                              copy($_FILES[file][tmp_name][$key], $add);
                              chmod("$add",0777);

                  }
            }

?>
0
 
tr5Author Commented:
Thanks. I got it to work.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now