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
tr5Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tr5Author Commented:
Thanks. I got it to work.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.