EM10
EM10

Reputation: 815

Uploading multiple files in server

I want to upload multiple files instead of just one. I know how to upload a single file (image) I need help with uploading multiple files. Below you can see how I do to upload a single file.

Here is my HTML when adding:

    <form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file" multiple><br>
    <input type="submit" name="submit" value="Submit">
    </form>

I have seen some people change name="file" to name="file[]". But I don't know how to upload it with php.

Here is the PHP code

   <?php
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);

    $random_digit=rand(0000,9999);

    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000)
    && in_array($extension, $allowedExts))
    {
    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
  {
  $file_name = $_FILES["file"]["name"];
  $ext = pathinfo($file_name, PATHINFO_EXTENSION); // Get the extension of a file
  $new_file_name = $random_digit . '.' . $ext;
  move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $new_file_name);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
     }
   }
 }
else
 {
 echo "Invalid file";
 }
 ?> 

Upvotes: 0

Views: 1844

Answers (2)

Loopo
Loopo

Reputation: 2195

Your upload form needs to have multiple input fields with the same name.

let's call these 'upload[]'

then in your receiving script, you need to loop through the $_FILES[] array in order to get to each one.

$response = "";
$i = 0;
while ((isset($_FILES['upload']['name'][$i])) && ($_FILES['upload']['name'][$i]<>"")){
    $error = $_FILES['upload']['error'][$i];

    if ($error == 0){

        $upfilename = $_FILES['upload']['name'][$i];
        //should probably validate your filename here
            //$upfilename = validate_fname($upfilename)

        $tmpname = $_FILES['upload']['tmp_name'][$i];
        $dirname = "uploads" // your files to be stored in this directory
        if (move_uploaded_file($tmpname, $dirname."/".$filename)){
            $response .= "<br><div class=\"message\">File : $filename : uploaded successfully. Thank you.</div><br>";
        }else {
            $response .= "<br> <div class=\"message\"> There was an error uploading file: $filename ";
        }

    }
    else $response = "<br><div class=\"errormsg\">there was an error - error code: $error</div><br>";
    $i++;
}

Upvotes: 1

aichingm
aichingm

Reputation: 796

if you use name="file[]" $_FILES["file"]["name"] will be an array() you can test it with print_r($_FILES["file"]) or var_dump($_FILES["file"]) your array will look like this:

Array
(
[uploads] => Array
    (
        [name] => Array
            (
                [0] => hack-attempt.sh
                [1] => picture.gif
                [2] => text.txt
                [3] => virus.exe
            )

        [type] => Array
            (
                [0] => application/sh
                [1] => image/jpeg
                [2] => text/plain
                [3] => applicatio/octet-stream
            )

        [tmp_name] => Array
            (
                [0] => /tmp/hack-attempt.sh
                [1] => /tmp/picture.gif
                [2] => /tmp/text.txt
                [3] => /tmp/virus.exe
            )

        [error] => Array
            (
                [0] => 0
                [1] => 0
                [2] => 0
                [3] => 0
            )

        [size] => Array
            (
                [0] => 217297
                [1] => 53600
                [2] => 6511924
                [3] => 127662
            )

    )

)

example taken from: http://staticfloat.com/php-programmieren/multiupload-mit-html5-und-php/ it is german but just for the credit.

Upvotes: 3

Related Questions