4

I have a file upload form set up with the HTML5 multiple attribute.

However, the form still only uploads a single file. Do i need to create some sort of a looping function in the php or is there another way of doing this?

Here's my code...

form:

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">        
       <input type="file" multiple="multiple" name="file[]" id="file" />
       <input name="submit" type="submit" value="Submit" />    
</form>

php:

<?php
if(isset($_POST['submit'])) {           
foreach($_FILES['newsImage'] as $file){ 
if ((($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {


    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
}
}
?>
3
  • post your form code also. it should contain a multi-part specification. Commented Jan 4, 2012 at 11:02
  • 2
    Your code seems to be processing only one file - are you sure the problem is on the upload side of things? Commented Jan 4, 2012 at 11:02
  • It is worth noting that IE (and Opera I think) does not support choosing multiple files through the <input type="file" /> element. Commented Jan 4, 2012 at 11:09

4 Answers 4

3

I believe your field should be <input type="file" multiple="multiple" name="files[]" />

And then in PHP:

<?php
   foreach($_FILES['files'] as $file){
       // Handle one of the uploads
   }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I've amended my code based on your reply, it doesn't seem to upload anything now though... any ideas... thanks
Inside your loop you are still calling $_FILES['file']['type'] etc... you should call $file['type'] (the specific file inside the loop over your files array).
3
for ($i = 0; $i < count($_FILES['newsImage']['name']); $i++) {
    // handle upload
}

3 Comments

Why did you accept your own answer which was answered two weeks after mine (which is the same solution ... ?) Seems a bit weird I have to admit (:
The foreach won't work due to the structure of the file array when using a multiple file input. You need to define each file info part as $_FILES['inputName']['name'][0] for the first filename, $_FILES['inputName']['name'][1] for the second, etc.
Although this is an old post, I've come across this issue as well. Indeed, uploading multiple files does make the array structure more complicated, thus using a regular for loop like this one really helps. I'm sure there is a way to use foreach, but this is simpler.
2

I believe this code could serve the purpose. It loops through the $_FILES array and creates an array with key => value pair of all the attributes for for each file.

$temp = array();
foreach ($_FILES['file'] as $key => $value) {
    foreach($value as $index => $val){
        $temp[$index][$key] = $val;
    }
}

Comments

-1
<?php   

    include 'db.php';

      extract($_POST);

        extract($_POST);
        if(isset($submit))
        {

          $count = count($_FILES['image']['name']);

           for($i=0;$i<$count;$i++)
           {
              $fname = $_FILES['image']['name'][$i];
              $file_tmp = $_FILES['image']['tmp_name'][$i];
              $file_size =  $_FILES['image']['size'][$i];
               $file_type=$_FILES['image']['type'][$i];
               echo $file_size,$file_type;         
               $target = "img/".$fname;
               move_uploaded_file($file_tmp,$target);
                echo "uploaded succ !"."<br>";

           }

        }

?>

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.