0

I have the following which uploads a single file and works fine:

<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    <input name="userfile" type="file" />
    <input type="submit" value="Upload" />
</form>
<?php

$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
  echo "File has been successfully uploaded.\n";
} else {
  echo "Upload failed";
}


?> 

When i adpat this to accept multiple files for upload, it doesnt seem to work. I dont get any errors / warnings so i am completely stumped. Here is my multiple file upload code:

<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    <input name="userfile[]" type="file" multiple />
    <input type="submit" value="Upload" />
</form>

<?php 

$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]);

foreach ($_FILES["userfile"]["error"] as $key => $error) { 
if ($error == UPLOAD_ERR_OK) { 
$name = $_FILES["userfile"]["name"][$key]; 
move_uploaded_file( $_FILES["userfile"]["tmp_name"][$key], $uploadfile); 
{
 echo "File has been successfully uploaded.\n";
 } else {
 echo "Upload failed";
 }
?>

Any suggestions on what could be wrong?

2
  • 1
    $_FILES["files"] is not the same as name="userfile[]" Commented Nov 4, 2013 at 10:16
  • Set your error_reporting and display_errors to levels that are sensible for developing … so that PHP can tell you about stuff like this itself! Commented Nov 4, 2013 at 10:20

2 Answers 2

1

You use wrong key in $_FILES, you have to use $_FILES['userfile'], not $_FILES['files']:

<form enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
    <input name="userfile[]" type="file" multiple />
    <input type="submit" value="Upload" />
</form>

<?php 

$uploaddir = $campaign['upload_dir'].'/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$key]);

foreach ($_FILES["userfile"]["error"] as $key => $error) { 
if ($error == UPLOAD_ERR_OK) { 
$name = $_FILES["userfile"]["name"][$key]; 
move_uploaded_file( $_FILES["userfile"]["tmp_name"][$key], $uploadfile); 
{
 echo "File has been successfully uploaded.\n";
 } else {
 echo "Upload failed";
 }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, i forgot to update this in the question. I have now edited it.
0
          <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <title>Multiple File Ppload with PHP</title>
    </head>
    <body>
      <form action="" method="post" enctype="multipart/form-data">
        <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
      <input type="submit" value="Upload!" />
    </form>
    </body>
    </html>


      <?php

         $valid_formats = array("jpg", "png", "gif", "zip", "bmp");
        $max_file_size = 1024*100; //100 kb
        $path = "uploads/"; // Upload directory
        $count = 0;

        if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
            // Loop $_FILES to exeicute all files
            foreach ($_FILES['files']['name'] as $f => $name) {     
                if ($_FILES['files']['error'][$f] == 4) {
                    continue; // Skip file if any error found
                }          
                if ($_FILES['files']['error'][$f] == 0) {              
                    if ($_FILES['files']['size'][$f] > $max_file_size) {
                        $message[] = "$name is too large!.";
                        continue; // Skip large files
                    }
                    elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
                        $message[] = "$name is not a valid format";
                        continue; // Skip invalid file formats
                    }
                    else{ // No error found! Move uploaded files 
                        if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                        $count++; // Number of successfully uploaded file
                    }
                }
            }
        }
?>

Comments

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.