PHP Multiple File Upload 🎯

beginner
15 min

PHP Multiple File Upload 🎯

Welcome to our comprehensive guide on PHP Multiple File Upload! By the end of this tutorial, you'll be able to handle multiple file uploads in your PHP projects. Let's dive in!

Understanding the Need πŸ“

In many real-world applications, you might need to upload multiple files at once. This could be a photo gallery, a file manager, or even a form where users can upload multiple documents.

Setting Up the Environment βœ…

Before we start, ensure you have PHP and a web server (e.g., Apache or Nginx) installed on your system. For this tutorial, we'll use a simple HTML form combined with PHP for handling the file uploads.

Basic Multiple File Upload πŸ’‘

HTML Form

Let's create a simple HTML form to upload multiple files.

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Multiple File Upload</title> </head> <body> <h1>Upload Multiple Files</h1> <form action="upload.php" method="post" enctype="multipart/form-data"> Select Files: <input type="file" name="files[]" multiple><br> <input type="submit" value="Upload Files"> </form> </body> </html>

PHP Script

Now, let's create the PHP script to handle the uploaded files.

php
<?php // Check if form is submitted if(isset($_POST['submit'])) { // Loop through each uploaded file foreach($_FILES['files']['tmp_name'] as $key => $tmpName) { // Get the file name $fileName = $_FILES['files']['name'][$key]; // Get the file extension $fileExt = pathinfo($fileName, PATHINFO_EXTENSION); // Allowable file extensions $allowedExts = array("jpg", "jpeg", "png", "gif"); // Check if the file extension is allowed if(in_array($fileExt, $allowedExts)) { // Move the uploaded file to the desired directory move_uploaded_file($tmpName, "uploads/$fileName"); } else { echo "Invalid file extension."; } } } ?>
Quick Quiz
Question 1 of 1

What is the purpose of the `enctype="multipart/form-data"` attribute in the HTML form?

Organizing Uploaded Files πŸ“

To keep our uploads organized, let's create a directory for the uploaded files. If the directory doesn't exist, we'll create it automatically.

php
// Create uploads directory if it doesn't exist if (!file_exists('uploads')) { mkdir('uploads', 0755, true); }

Validating Uploaded Files πŸ’‘

It's important to validate the uploaded files to ensure they meet certain criteria such as maximum size, allowed file types, and more.

php
// File name validation $fileName = $_FILES['files']['name'][$key]; $fileExt = pathinfo($fileName, PATHINFO_EXTENSION); // Maximum file size (in bytes) $maxSize = 1000000; // 1MB // File size validation if ($_FILES['files']['size'][$key] > $maxSize) { echo "File size is too large."; }

Wrapping Up πŸ“

You now have a solid understanding of PHP Multiple File Uploads! You can extend this knowledge by exploring more advanced topics such as handling different file types, creating thumbnails, and storing metadata.

Remember, practice makes perfect! Keep experimenting and improving your skills. Happy coding! πŸš€