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>";
 }
}
?>
  • Share/Bookmark

24 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.

  9. Abbie said,

    on October 12th, 2008 at 9:16 am

    Why is it limited to 10 because you can make it upload 10 files easily.apart from that this is a fantastic peace of code!
    xxx

  10. jose said,

    on October 24th, 2008 at 8:33 pm

    Great code, how caI upload into my sql?

  11. steve said,

    on December 28th, 2008 at 1:11 pm

    Well Done! Thanks…

  12. erebus said,

    on February 5th, 2009 at 5:25 pm

    wtf…
    this is NOT like Gmail functionality.
    Here you need the user to TELL you how many files he wants to upload.
    And that is NOT a good idea.
    But well… this was posted in 2006. D:

  13. Aditya said,

    on March 8th, 2009 at 1:36 pm

    Thank you :) Came in very handy…

  14. Nelson said,

    on March 11th, 2009 at 10:36 am

    This tutorial's simple yet does the trick coolly! I hope improvements would be made in the future such that the file format is filtered as well as its size (and dimensions, if applicable).

    Thank you very much!

  15. mmx said,

    on April 13th, 2009 at 7:19 am

    i want to up load my files as zip then extract and save that file to my host ???????????/ help me please

  16. Matt said,

    on May 18th, 2009 at 1:29 pm

    I want 1.2 million dollars… can you modify the script to do that too? Hahaha… nice tutorial man!

  17. CARlos NARez said,

    on June 9th, 2009 at 1:08 pm

    Thanks, this works like a charm!

  18. Pembo said,

    on July 23rd, 2009 at 3:13 pm

    This is definately the coolest and most easy to use script I have ever found for multiple uploads.

    Thank You!!!
    Pembo

  19. prathiba said,

    on August 5th, 2009 at 5:04 am

    hi… your code really worked fine for me but regarding database can u help me .. how can i insert the multiple records for a single user..

  20. prathiba said,

    on August 5th, 2009 at 5:15 am

    please can u send me the correct code for multiple file operation..its really urgent for me

  21. Rajib said,

    on November 23rd, 2009 at 1:16 am

    it's a bad idea to ask the user to enter the number of files he/she wants to upload.

  22. Mr. Beginner said,

    on January 25th, 2010 at 1:16 am

    File 1:

    File 2:

    Is the code above alright? I have not put any trappings on it yet, but ah… I guess it has the most basic idea on how you could upload multiple files and renaming it while uploading.

    And yeah, I should make an "upload" directory first before I run this code…

  23. Curtis said,

    on February 10th, 2010 at 11:58 am

    Got one for ya. Can you have the same script but resize the images plus add them into your sql database with a description. That I find is a little tricky

  24. Muktesh said,

    on June 23rd, 2010 at 2:10 am

    Thanx,

    it was really useful to me.

    its easy one.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word