Please wait

Uploading Multiple Files

It's completely possible to upload multiple files at once under a single name. Allowing multiple files to be uploaded at once is not uncommon. Let's look at a basic example.

Preparing the Client

Preparing the client to upload multiple files involves creating a proper HTML form that allows the user to select and send multiple files to the server. Take a look at the following.

<form action="upload.php" method="post" enctype="multipart/form-data">
  <label for="files">Select files:</label>
  <input type="file" name="images[]" id="files" multiple />
  <input type="submit" value="Upload Files" />
</form>

Most of this is familiar to us. The are two differences from before, which are all on the <input> element. Firstly, in the input element for files, add the multiple attribute, which allows users to select more than one file. Secondly, the name attribute should be an array (e.g., name="files[]") to accommodate multiple files. It's important to add the [] characters after the name.

Handling the Upload with PHP

This form will allow users to select multiple files from their system and submit them all at once to the server. The server-side PHP code will then handle these files through the $_FILES array, and you can iterate over this array to process each uploaded file.

In our case, the $_FILES['images'] value will be an array of all the uploaded files. Each array will have the name, type, tmp_name, error, and size keys for each file.

Here's an example of PHP code to handle multiple file uploads:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  // Count the number of uploaded files
  $totalFiles = count($_FILES['images']['name']);
 
  // Loop through each file
  for ($i = 0; $i < $totalFiles; $i++) {
    // Get the file's temporary location
    $tmpFilePath = $_FILES['images']['tmp_name'][$i];
 
    // Make sure the file has a temporary location
    if ($tmpFilePath != "") {
      // Set the destination path for the uploaded file
      $newFilePath = "./uploads/" . $_FILES['images']['name'][$i];
 
      // Move the file to the destination
      if (move_uploaded_file($tmpFilePath, $newFilePath)) {
        echo "File " . $_FILES['images']['name'][$i] . " uploaded successfully!<br>";
      } else {
        echo "Error uploading " . $_FILES['images']['name'][$i] . "<br>";
      }
    }
  }
}

Here's a breakdown of what's going on:

  1. Since multiple files are uploaded, you loop through the $_FILES array using a for loop to handle each file individually.
  2. Each uploaded file is initially stored in a temporary location, retrieved with $_FILES['files']['tmp_name'][$i].
  3. Next, we defined where we wanted to store the file on our server. Here, it's in an "uploads" directory with the original filename.
  4. Use move_uploaded_file() to move the file from the temporary location to the destination.
  5. As an extra precaution, we added basic error handling with simple messages to help understand what's happening during the upload process.

Make sure that in a real-world scenario, additional validations such as file size, type, and name sanitization are typically performed to enhance security and functionality.

Key Takeaways

  • Use an HTML form with an input field with the multiple attribute and array notation for the name (e.g., name="files[]").
  • In PHP, access the files through the $_FILES array, iterating over it to process each file individually.

Comments

Please read this before commenting