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.