How to upload multiple files and store them in a folder with PHP?

Below are the steps to upload multiple files and store them in a folder −

  • Input name must be defined as an array i.e. name="inputName[]"
  • Input element should have multiple="multiple" or just multiple
  • In the PHP file, use the syntax "$_FILES['inputName']['param'][index]"
  • Empty file names and paths have to be checked for since the array might contain empty strings. To resolve this, use array_filter() before count.

Below is a demonstration of the code −


<input name="upload[]" type="file" multiple="multiple" />


$files = array_filter($_FILES['upload']['name']); //Use something similar before processing files.
// Count the number of uploaded files in array
$total_count = count($_FILES['upload']['name']);
// Loop through every file
for( $i=0 ; $i < $total_count ; $i++ ) {
   //The temp file path is obtained
   $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
   //A file path needs to be present
   if ($tmpFilePath != ""){
      //Setup our new file path
      $newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
      //File is uploaded to temp dir
      if(move_uploaded_file($tmpFilePath, $newFilePath)) {
         //Other code goes here

The files are listed and the count of the number of files that need to be uploaded is stored in ‘total_count’ variable. A temporary file path is created and every file is iteratively put in this temporary path that holds a folder.