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

In PHP, you can upload multiple files simultaneously by creating a file input with array notation and processing each file using the $_FILES superglobal. This approach allows users to select and upload several files in one form submission.

HTML Form Setup

To enable multiple file uploads, the HTML input must use array notation and the multiple attribute ?

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input name="upload[]" type="file" multiple="multiple" />
    <input type="submit" value="Upload Files" />
</form>

Key Requirements

  • Input name must be defined as an array i.e. name="inputName[]"
  • Input element should have multiple="multiple" or just multiple
  • Form must have enctype="multipart/form-data"
  • In PHP, use the syntax "$_FILES['inputName']['param'][index]"
  • Use array_filter() to remove empty file entries before processing

PHP Processing Code

Process the uploaded files by iterating through the $_FILES array and moving each file to the destination folder ?

<?php
// Filter out empty file names
$files = array_filter($_FILES['upload']['name']);

// Count the number of uploaded files
$total_count = count($_FILES['upload']['name']);

// Create upload directory if it doesn't exist
if (!is_dir('./uploadFiles/')) {
    mkdir('./uploadFiles/', 0777, true);
}

// Loop through every file
for($i = 0; $i < $total_count; $i++) {
    // Get the temp file path
    $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
    
    // Check if file was actually uploaded
    if ($tmpFilePath != "" && $_FILES['upload']['error'][$i] === UPLOAD_ERR_OK) {
        // Setup new file path
        $fileName = basename($_FILES['upload']['name'][$i]);
        $newFilePath = "./uploadFiles/" . $fileName;
        
        // Move uploaded file to destination
        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
            echo "File " . $fileName . " uploaded successfully.<br>";
        } else {
            echo "Failed to upload " . $fileName . ".<br>";
        }
    }
}
?>

Error Handling

Always check for upload errors and validate file types for security ?

<?php
$allowedTypes = ['jpg', 'jpeg', 'png', 'gif', 'pdf'];
$maxFileSize = 5 * 1024 * 1024; // 5MB

for($i = 0; $i < count($_FILES['upload']['name']); $i++) {
    $fileName = $_FILES['upload']['name'][$i];
    $fileSize = $_FILES['upload']['size'][$i];
    $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
    
    // Validate file type
    if (!in_array($fileExtension, $allowedTypes)) {
        echo "Invalid file type for " . $fileName . ".<br>";
        continue;
    }
    
    // Validate file size
    if ($fileSize > $maxFileSize) {
        echo "File " . $fileName . " is too large.<br>";
        continue;
    }
    
    // Process valid file...
}
?>

Conclusion

Multiple file uploads in PHP require proper HTML form setup with array notation and the multiple attribute. Always validate files for security and handle errors gracefully to ensure reliable file upload functionality.

Updated on: 2026-03-15T08:30:49+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements