Multiple files upload at once with PHP

In PHP, it is possible to upload multiple files using a single input file element. You just need to customize your single file upload PHP code and enable your file element to select multiple files.

In this tutorial, I show you how to implement multiple files upload with PHP.

Multiple files upload at once with PHP


Contents

  1. HTML
  2. PHP
  3. Completed Code
  4. Demo
  5. Conclusion

1. HTML

Here, are some steps to enable multiple files uploads in <form>.

Steps

  • First, create a <form> and add enctype='multiple/form-data' attribute.
<form method='post' action='' enctype='multipart/form-data'>
  • Create an input type='file' element and for enabling multiple files selection add multiple attribute. For reading all selected files when <form> submitted add [] brackets at the end of a name which denotes an Array.
<input type="file" name="file[]" id="file" multiple>
  • Add a submit button.

Completed Code

<form method='post' action='' enctype='multipart/form-data'>
 
 <input type="file" name="file[]" id="file" multiple>
 <input type='submit' name='submit' value='Upload'>

</form>

2. PHP

When <form > method submitted first count selected files and loop through all files. Select files on the bases of index.

Completed Code

<?php 
if(isset($_POST['submit'])){
 // Count total files
 $countfiles = count($_FILES['file']['name']);
 
 // Looping all files
 for($i=0;$i<$countfiles;$i++){
   $filename = $_FILES['file']['name'][$i];
   
   // Upload file
   move_uploaded_file($_FILES['file']['tmp_name'][$i],'upload/'.$filename);
    
 }
} 
?>

3. Completed Code

<?php 
if(isset($_POST['submit'])){
 
 // Count total files
 $countfiles = count($_FILES['file']['name']);

 // Looping all files
 for($i=0;$i<$countfiles;$i++){
  $filename = $_FILES['file']['name'][$i];
 
  // Upload file
  move_uploaded_file($_FILES['file']['tmp_name'][$i],'upload/'.$filename);
 
 }
} 
?>
<form method='post' action='' enctype='multipart/form-data'>
 <input type="file" name="file[]" id="file" multiple>

 <input type='submit' name='submit' value='Upload'>
</form>

4. Demo

View Demo


5. Conclusion

I didn’t apply any restriction in the upload PHP script but it’s better to add restriction when you are implementing this in your project.

If you are using more than one file element for a single task then you can use the above PHP script as a replacement.

Make sure to check upload_max_filesize and post_max_size values in the php.ini file if you are allowing large files to upload.

If you found this tutorial helpful then don't forget to share.

29 thoughts on “Multiple files upload at once with PHP”

  1. Dear Singh,how to make for each customer have their own folder for upload,..e.g..new customer-new name-new folder for images , et cetera….Regards Zex

    Reply
  2. I want to three uploads button and a submit button to click and temporarily store the file in the server and then save the the file in a folder, then the path in a database.
    Please help me, how do i go by that

    Reply
  3. How to include the file type, size and error checks to your code?
    I want if all files are from allowed extension then upload it otherwise no files shall be uploaded even if some of the files are from allowed extension

    Reply
    • Hi Adnan,
      For this, you need to create a boolean variable and assign true to it. Loop on all files and check for an extension. If an extension is not valid then assign false to a boolean variable and break the loop.

      If a boolean variable has true value then again loop on the files and upload it.

      Reply
  4. I can get this to work great while it stands alone. I can upload files to the folder i designate on my server.

    my issue begins when I add this to my already existing form that emails the information back to me once it is filled out. I want to have the option to upload files (photos) and and continue sending me the email as usual. i think it has to do with the fact that it already has a command to email me when the ‘submit’ is clicked. I am a complete novice and am struggling with this.

    Reply
  5. I tried this code for uploading multiple images using camera.
    I add capture =”camera” in the input field.
    But it works for a single image
    I want to use your code for multiple images.
    Help me.

    Reply
  6. Hi, how can I move files into phpmyadmin and display it on index pag ? I need to do a document management system for school and i need help . plsss

    Reply
  7. I have searched many articles but not found 3 (upload inputs) different file upload by checking file size and type validation in php mysql

    Reply
  8. $countfiles = count($_FILES[‘file’][‘name’]);

    This only counts to 20…
    Even if you put more files and it shows 21 files but it only uploads 20
    And i have put allowed file size to 1000000000

    Reply
  9. Great script. If anyone want to check for file size and extension, use following conditions:
    $file_size = $_FILES[‘file’][‘size’];
    $file_type = pathinfo($file, PATHINFO_EXTENSION);

    Reply

Leave a Comment