How to Upload Multiple Attachment Files in PHP

Sovary August 2, 2022 602
2 minutes read

PHP allows you to upload files or images to the server with minimum code. In this article, we'll guide you how to upload files in PHP and create a PHP script to upload files to a server directory.

You can upload any sorts of files, including photos, to the server with PHP using this sample PHP file upload script. This might be good for student project assignment for very basic beginner.

How to Upload Multiple Files in PHP

These following steps to make upload file possible.

  • Step 1 - Create Simple HTML Form Upload
  • Step 2 - Create PHP Upload Script File

Step 1 - Create Simple HTML Form Upload

First, you have to create a form that allow user browse files from their computer into browsers (Chrome, Safari, Firefox...). Basically, form to upload file make sure there are following required attributes.

  • method="post"
  • enctype="multipart/form-data"

Before go further make sure yourself understand basic HTML and PHP. Here is the code that is important point in whole HTML page.

<form action="upload.php" method="post" enctype="multipart/form-data">
   Browse File to Upload:
   <input type="file" name="files[]" multiple>
   </br>
   <input type="submit" name="upload" value="Upload">
</form>

HTML element type="file" help browser to translated as button browse file in computer, while files[] is variable to store temporally value of multiple file in HTML element. multiple attribute is to allow user select multiple files at the same time.

This form will be submitted to the server-side script file name upload.php for uploading the selected files to the server. The below HTML code is full snippet code.

Create file index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Upload File</title>
  </head>
  <body>
      <h1 class="title">Upload File - CamboTutorial.com </h1>

      <form action="upload.php" method="post" enctype="multipart/form-data">
          Browse File to Upload:
          <input type="file" name="files[]" multiple>
          </br>
          <input type="submit" name="upload" value="Upload">
      </form>

  </body>
</html>

Step 2 - Create PHP Upload Script File

This step we will create a PHP file to write upload file script, as well known server side to handle the file upload process using raw PHP. The following functions are important working for upload files.

  • is_dir() - to check whether is directory or not
  • mkdir() - to create directory
  • move_uploaded_file() - to move file upload from temporally file to actually directory upload (upload file).

Now below script to upload multiple file with server code.

Create file upload.php

<?php

if(isset($_FILES))
{
    // Count total files
    $countfiles = count($_FILES['files']['name']);
    $dir = "upload/";
    // if there no directory upload then create once
    if(!is_dir($dir))mkdir($dir);
    // Looping all files
    for($i=0; $i< $countfiles; $i++)
    {
        // Get each file by index
        $filename = $_FILES['files']['name'][$i];
        // Upload file
        move_uploaded_file($_FILES['files']['tmp_name'][$i],$dir.$filename);
    }
    echo "Uploaded $countfiles file(s)!";
}
else
{
    echo "No file to upload";
}
?>

If you found this tutorial helpful then don't forget to share. Please have a nice day!

You might also like..

 

PHP 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him