PHP Code

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>";


  1. 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. Modify this line I believe

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

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

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

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

  5. 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. one question
    how can I donร‚ยดt show the folders "." and ".."

    sorry my english

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

  8. Jonathan Wagener

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

  9. 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. Great code, how caI upload into my sql?

  11. Well Done! Thanks…

  12. 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. Thank you ๐Ÿ™‚ Came in very handy…

  14. 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. i want to up load my files as zip then extract and save that file to my host ???????????/ help me please

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

  17. Thanks, this works like a charm!

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

    Thank You!!!

  19. 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. please can u send me the correct code for multiple file operation..its really urgent for me

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

  22. 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. 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. Thanx,

    it was really useful to me.

    its easy one.

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

  26. Hi,
    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. 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. very neat,clean & simple code

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

  30. David Buchweitz

    Why not just loop through $_FILES ?

  31. Is there a way to restrict file types?

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

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

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