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

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

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

<title>Upload files</title>

<form name="form1" enctype="multipart/form-data" method="post" action="uploadFiles.php">
  $uploadsNeeded = $_POST&#91;'uploadsNeeded'&#93;;
  for($i=0; $i < $uploadsNeeded; $i++){
    <input name="uploadFile<? echo $i;?>" type="file" id="uploadFile<? echo $i;?>" />
  <? } ?>
  <p><input name="uploadsNeeded" type="hidden" value="<? echo $uploadsNeeded;?>" />
    <input type="submit" name="Submit" value="Submit" />

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

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

36 Responses to Upload Multiple Files at Once with PHP

  1. Luis says:

    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.


  2. Justin Cook says:

    Modify this line I believe

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

  3. Luis says:

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

  4. Justin Cook says:

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

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

  5. Luis says:

    Hi, I did:

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


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


  6. Luis says:

    one question
    how can I donร‚ยดt show the folders "." and ".."

    sorry my english

  7. Justin Cook says:

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

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

  9. Abbie says:

    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!

  10. jose says:

    Great code, how caI upload into my sql?

  11. steve says:

    Well Done! Thanks…

  12. erebus says:

    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 says:

    Thank you ๐Ÿ™‚ Came in very handy…

  14. Nelson says:

    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 says:

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

  16. Matt says:

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

  17. CARlos NARez says:

    Thanks, this works like a charm!

  18. Pembo says:

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

    Thank You!!!

  19. prathiba says:

    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 says:

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

  21. Rajib says:

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

  22. Mr. Beginner says:

    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 says:

    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 says:


    it was really useful to me.

    its easy one.

  25. Nitin Deepak says:

    The Code was really triky. I am loving it…..

  26. hirako says:

    This is a really useful snippet of code.
    I have made a small project that uploads couple of files
    and processes them, with your consent I was hoping to
    load my project on github with your code snippets,
    with a clear reference to your website in the Readme of the Project.
    Let me know if that works or I will then go on without using
    any code snippet that you have written.

  27. DragonStar says:

    Execuse me, sir.
    I want to konw about to upload multiple files with single click a main document,,,
    Then the pictures in that document will upload, together…
    Help Me!!!

  28. Rohit says:

    very neat,clean & simple code

  29. Suresh says:

    Thaks ๐Ÿ™‚ It's working well and very useful.

  30. Why not just loop through $_FILES ?

  31. Cookie Monster says:

    Is there a way to restrict file types?

  32. vinod says:

    Thank you, i searched this script by one week…..

  33. wan says:

    its 2011 and thanks justin-cooks..
    nothing more than a simple, clean, easy to maintain and very very good example ๐Ÿ˜‰
    thanks mate..

  34. jeetendra says:

    thanks for the answer but i think it will create multiple different file input
    it seems good if it will produce only one file element for uploading the content

  35. Xaninho99 says:

    the code is cool but have some bugs.
    for example if we upload 0 pics
    if we put to upload 3 fields and one of them is not with any file
    in prompt if we want do upload 12 files there we can upload 99 in fieldlength( thats normal we put 2 fields if we want 12 files we press 2 times to upload ๐Ÿ™‚ )
    and doesnt have any kind of max size upload file or kaind of extencion.

    but really cool code dude, easy simple to use and debug and to add those things i said

    4/5 *

  36. Varsha says:

    Hi, I am facing an issue with the copy function

    Error m getting is :- [Wed May 23 18:30:06 2012] [error] PHP Warning: copy(elct_bill.JPG) [function.copy]: failed to open stream: No such file or directory in image_upload.php on line 15, referer: uploadform.php

    Please help

Leave a Reply

Your email address will not be published. Required fields are marked *

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.
Anti-spam image