Multiple files upload at once with PHP

Whether you’re creating a file-sharing platform, a social media website, or any other type of web application, the ability to upload multiple files at once can significantly improve user experience.

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 validation in PHP.

Multiple files upload at once with PHP


Contents

  1. HTML – Create Multiple File upload <form >
  2. PHP – Upload Multiple selected files
  3. Completed Code
  4. Demo
  5. Conclusion

1. HTML – Create Multiple File upload <form >

Here, are the steps to enable multiple file 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 that 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 – Upload Multiple selected files

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

I am storing files to uploads folder. Assign file name with upload path to $location, assign file extension to $extension.

Initialize $valid_extensions Array with allowed file upload extensions. If extension exists in $valid_extensions Array then upload the file.

Completed Code

<?php

if(isset($_POST['submit'])){

    $countfiles = count($_FILES['file']['name']);

    $totalFileUploaded = 0;
    for($i=0;$i<$countfiles;$i++){
         $filename = $_FILES['file']['name'][$i];

         ## Location
         $location = "uploads/".$filename;
         $extension = pathinfo($location,PATHINFO_EXTENSION);
         $extension = strtolower($extension);

         ## File upload allowed extensions
         $valid_extensions = array("jpg","jpeg","png","pdf","docx");

         $response = 0;
         ## Check file extension
         if(in_array(strtolower($extension), $valid_extensions)) {
              ## Upload file
              if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$location)){

                   echo "file name : ".$filename."<br/>";

                   $totalFileUploaded++;
              }
         }

    }
    echo "Total File uploaded : ".$totalFileUploaded;
}

3. Completed Code

<!doctype html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multiple files upload at once with php</title>
</head>
<body >
    
    <span style='color: red;margin-bottom:8px; margin-top:10px;'>NOTE - Files not upload to the server only displaying names of file. </span><br/>
    <form method='post' action='' enctype='multipart/form-data'>
         <input type="file" name="file[]" id="file" multiple><br/><br/>

         <input type='submit' name='submit' value='Upload'>
    </form>
    <?php
    if(isset($_POST['submit'])){

         $countfiles = count($_FILES['file']['name']);

         $totalFileUploaded = 0;
         for($i=0;$i<$countfiles;$i++){
              $filename = $_FILES['file']['name'][$i];

              ## Location
              $location = "uploads/".$filename;
              $extension = pathinfo($location,PATHINFO_EXTENSION);
              $extension = strtolower($extension);

              ## File upload allowed extensions
              $valid_extensions = array("jpg","jpeg","png","pdf","docx");

              $response = 0;
              ## Check file extension
              if(in_array(strtolower($extension), $valid_extensions)) {
                   ## Upload file
                   if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$location)){

                         echo "file name : ".$filename."<br/>";

                         $totalFileUploaded++;
                   }
              }

         }
         echo "Total File uploaded : ".$totalFileUploaded;
    }
    ?>
</body>
</html>

4. Demo

View Demo


5. Conclusion

You have to add [] with the file name and multiple attribute in the HTML and access the selected file using the index position and perform file upload operation using PHP.

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.

30 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 Reply to Mawuli Cancel reply