Home > Backend Development > PHP Tutorial > How to Implement Multi-File Upload using HTML and PHP?

How to Implement Multi-File Upload using HTML and PHP?

Barbara Streisand
Release: 2024-12-16 10:38:10
Original
174 people have browsed it

How to Implement Multi-File Upload using HTML and PHP?

Multi-File Upload with HTML and PHP via HTTP POST

Uploading multiple files simultaneously can enhance the user experience for tasks such as image selection and bulk operations. While traditional single file uploads are familiar with , expanding this capability to multiple files requires a modified approach.

HTML5 introduced the multiple attribute for , allowing users to select multiple files in the file dialog. To handle this in the PHP backend, it's essential to use the multipart/form-data encoding type.

The following PHP and HTML code sample demonstrates multi-file upload:

<!doctype html>
<html>
<body>
    <form method="post" enctype="multipart/form-data">
        <input type="file" name="my_file[]" multiple>
        <input type="submit" value="Upload">
    </form>
</body>
</html>
Copy after login

In the PHP script:

if (isset($_FILES['my_file'])) {
    $myFile = $_FILES['my_file'];
    $fileCount = count($myFile["name"]);

    for ($i = 0; $i < $fileCount; $i++) {
        echo "<p>File #".($i+1).":</p>";
        echo "<p>Name: ".$myFile["name"][$i]."<br>";
        echo "Temporary file: ".$myFile["tmp_name"][$i]."<br>";
        echo "Type: ".$myFile["type"][$i]."<br>";
        echo "Size: ".$myFile["size"][$i]."<br>";
        echo "Error: ".$myFile["error"][$i]."<br>";
    }
}
Copy after login

This script iterates through the uploaded files, providing information on each file's name, temporary location, type, size, and any errors encountered. It's important to follow proper file handling techniques for security and efficiency, such as verifying the file's source, performing virus checks, and properly storing the files on the server.

The above is the detailed content of How to Implement Multi-File Upload using HTML and PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template