July 17th, 2006

Upload Multiple Files at Once with PHP

With PHP, it's fairly simple to include a file field in a form to upload one file. But what if you need to upload multiple files at one time? (Like the attach file functionality in Gmail). This can be easily facilitated with the use of the 'for' loop. Here's the code to do it!

The first thing you'll need to do is prompt the user for how many files they need to upload. That will determine how many file fields we need to present. This method creates the fields on a second page, but I recommend you create the fields in real-time with DHTML, to prevent a post-back. But for this example, simplicity is key. Here's the first step, call it "prompt.html".

note - we set the max to 9 to prevent server overloading. You can modify that if you need

<html>
<head>
<title>Choose Number of Files to Upload</title>
</head>
<body>
<form name="form1" method="post" action="uploadForm.php">
  <p>How many files would you like to upload? (Max = 9).</p>
  <p>
    <input name="uploadsNeeded" type="text" id="uploadsNeeded" maxlength="1" />
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
</body>
</html>

Here's the code for uploadForm.php, where it will create as many file fields as the user has specified.

<html>
<head>
<title>Upload files</title>
</head>
<body>

<form name="form1" enctype="multipart/form-data" method="post" action="uploadFiles.php">
  <p>
  <?
  $uploadsNeeded = $_POST['uploadsNeeded'];
  for($i=0; $i < $uploadsNeeded; $i++){
  ?>

    <input name="uploadFile<? echo $i;?>" type="file" id="uploadFile<? echo $i;?>" />
  </p>
  <? } ?>
  <p><input name="uploadsNeeded" type="hidden" value="<? echo $uploadsNeeded;?>
" />
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
</body>
</html>

And finally, here's the code for 'uploadFiles.php', which loops through and processes each field;

<?
$uploadsNeeded = $_POST['uploadsNeeded'];
for($i = 0; $i < $uploadsNeeded; $i++){
$file_name = $_FILES['uploadFile'. $i]['name'];
// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$copy = copy($_FILES['uploadFile'. $i]['tmp_name'],$file_name);
 // prompt if successfully copied
 if($copy){
 echo "$file_name | uploaded sucessfully!<br>";
 }else{
 echo "$file_name | could not be uploaded!<br>";
 }
}
?>

8 Responses to ' Upload Multiple Files at Once with PHP '

Subscribe to comments with RSS or TrackBack to ' Upload Multiple Files at Once with PHP '.

  1. Luis said,

    on June 26th, 2007 at 5:18 pm

    Hi, one question, i´m use this code is very very nice, but i have a problem, where o how can I change the directory where the files go to the server?, I see the it put the files in the root of uploadfile.php but a need change the directory destination.

    Thanks!

  2. Justin Cook said,

    on June 26th, 2007 at 5:32 pm

    Modify this line I believe

    $copy = copy($_FILES['uploadFile'. $i]['tmp_name'],$file_name)

  3. Luis said,

    on June 26th, 2007 at 6:31 pm

    yea, but how? change "tmp_name" ???? I try many ways but fails, thanks for your answer

  4. Justin Cook said,

    on June 26th, 2007 at 8:03 pm

    // Where the file is going to be placed
    $target_path = "uploads/";

    $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    $_FILES['uploadedfile']['tmp_name'];

  5. Luis said,

    on June 26th, 2007 at 8:19 pm

    Hi, I did:

    Modify this line:
    $copy = copy($_FILES['uploadFile'. $i]['tmp_name'],$file_name);

    TO

    $copy = copy($_FILES['uploadFile'. $i]['tmp_name'],"uploads/". $file_name);

    Thanks!!

  6. Luis said,

    on June 27th, 2007 at 6:02 pm

    one question
    how can I don´t show the folders "." and ".."

    sorry my english

  7. Justin Cook said,

    on June 27th, 2007 at 9:36 pm

    Just use a simple if statement to check for those names before displaying them


  8. on January 1st, 2008 at 1:37 pm

    Hi, interesting script, but what about using zip files? I have been looking for a script that does that.

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image