rws907
rws907

Reputation: 777

Iterating through multiple upload $_FILES array

I have two forms on my site, one that uploads a single file, and one that uploads multiple files. Both forms upload to the same directory and the single file form is working flawlessly. In an effort to optimize the code for the multiple file upload form so that I'm not writing out the processing of each file individually I have the following code:

foreach($_FILES['multi']['name'] as $uploaded_file){
     if(!move_uploaded_file($uploaded_file, "/var/www/uploads/".$uploaded_file."")){
         echo "Could not move File: ".$uploaded_file."<br />";
     }else{
         // Do additional processing
     }  
}

Instead of moving the files to the directory, that is valid and works for the single file upload form, the script errors out and displays my stop message:

"Could not move File: <filename here>"

Been scratching my head most of the nice, thanks in advance to any insight and help!

Upvotes: 1

Views: 820

Answers (2)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

try something like:

$count=0; 
foreach($_FILES['multi']['name'] as $uploaded_file) {
   $tmp=$_FILES['file']['tmp_name'][$count];
   if(!move_uploaded_file($tmp, "/var/www/uploads/".$uploaded_file)){
     echo "Could not move File: ".$uploaded_file."<br />";
   }else{
     // Do additional processing
   }   
   $count=$count + 1;
}

Upvotes: 0

Ja͢ck
Ja͢ck

Reputation: 173562

You're not targeting the temporary file location of the upload properly:

foreach ($_FILES['multi']['name'] as $idx => $uploaded_file) {
    move_uploaded_file($_FILES['multi']['tmp_name'][$idx], "/var/www/uploads/$uploaded_file");
}

Upvotes: 1

Related Questions