1

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!

1
  • Permissions are write because the single file upload form writes to the same directory and has no problem. Commented Nov 29, 2012 at 3:26

2 Answers 2

1

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");
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}

1 Comment

Same result, it simply outputs "Cannot Move File" for each file in the form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.