Multiple File Upload In PHP
Last Updated : Jul 1, 2023
IN - PHP HTML CSS | Written & Updated By - Riya
In this tutorial we will show you the solution of multiple file upload in PHP, file upload is the most frequent PHP feature that almost every PHP developer must implement.
As we all know, almost every webpage requires the ability to upload files.
Nowadays, whether it be on Instagram, Twitter, Facebook, or Tumblr, a user can't live without sharing files. Using various functionalities, you can select numerous files at once and upload them to a folder.
This tutorial will teach you how to upload multiple files using PHP.
Step By Step Guide On Multiple File Upload In PHP :-
Create HTML Page
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <form action="upload.php" method="POST" enctype="multipart/form-data"> <h2>Multiple Files Upload in PHP language</h2> <label for="files">Select files to upload</label><br> <input type="file" name="files[]" multiple><br> <input type="submit" name="submit" value="Upload"> </form> </body> </html>
- Make a simple HTML page and an HTML form for submitting files. That HTML form will include an input box where you can select files to upload and then submit them to the server.
- On the HTML page with the form, there is an input box and an upload button.
Make a style.css file.
form { background: #d2dfdf; padding: 20px; border-radius: 5px; width: 50%; margin: 0 auto; } form h2 { color: #3c7727; font-size: 28px; margin: 0 0 20px 0; } form input, form label { display: block; font-size: 16px; } input[type="submit"] { background: #0087ff; border: none; padding: 10px 20px; color: #fff; border-radius: 5px; cursor: pointer; }
- Because the design of the index.html file with CSS is totally up to you, this is an optional step.
- You can use your template to create a design. So this is simply a practise run to see how our HTML form looks and functions.
- Make a style.css file and save it with the CSS code below.
- You may also apply this CSS style inside the <head> tag by using the <style>/style> tag in your index.html file.
Create the upload.php File
<?php if(isset($_POST['submit'])) { $upload_dir = 'uploads'.DIRECTORY_SEPARATOR; $allowed_types = array('jpg', 'png', 'jpeg', 'gif'); $maxsize = 5 * 1024 * 1024; if(!empty(array_filter($_FILES['files']['name']))) { foreach ($_FILES['files']['tmp_name'] as $key => $value) { $file_tmpname = $_FILES['files']['tmp_name'][$key]; $file_name = $_FILES['files']['name'][$key]; $file_size = $_FILES['files']['size'][$key]; $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); $filepath = $upload_dir.$file_name; if(in_array(strtolower($file_ext), $allowed_types)) { if ($file_size > $maxsize) echo "Error: File size is too larg than the allowed limit."; if(file_exists($filepath)) { echo "{$file_name} already exists"; } else { if( move_uploaded_file($file_tmpname, $filepath)) { echo "{$file_name} successfully uploaded"; } else { echo "Error: uploading {$file_name}"; } } } else { echo "{$file_ext} This file type is not allowed"; } } } else { echo "No files selected."; } } ?>
- Create a PHP file to add uploading functionality (upload.php). The action property will call this file when the form is submitted with the option to upload files.
- It will initially check to see if the submit button has been touched before moving on to the upload file feature.
Uploading Multiple Files
<?php session_start(); require_once __DIR__ . '/inc/flash.php'; require_once __DIR__ . '/inc/functions.php'; const ALLOWED_FILES = [ 'image/png' => 'png', 'image/jpeg' => 'jpg' ]; const MAX_SIZE = 5 * 1024 * 1024; // 5MB const UPLOAD_DIR = __DIR__ . '/uploads'; $is_post_request = strtolower($_SERVER['REQUEST_METHOD']) === 'post'; $has_files = isset($_FILES['files']); if (!$is_post_request || !$has_files) { redirect_with_message('Invalid file upload operation', FLASH_ERROR); } $files = $_FILES['files']; $file_count = count($files['name']); $errors = []; for ($i = 0; $i < $file_count; $i++) { $status = $files['error'][$i]; $filename = $files['name'][$i]; $tmp = $files['tmp_name'][$i]; if ($status !== UPLOAD_ERR_OK) { $errors[$filename] = MESSAGES[$status]; continue; } $filesize = filesize($tmp); if ($filesize > MAX_SIZE) { $message = sprintf("The file %s is %s which is greater than the allowed size %s", $filename, format_filesize($filesize), format_filesize(MAX_SIZE)); $errors[$filesize] = $message; continue; } if (!in_array(get_mime_type($tmp), array_keys(ALLOWED_FILES))) { $errors[$filename] = "The file $filename is allowed to upload"; } } if ($errors) { redirect_with_message(format_messages('The following errors occurred:',$errors), FLASH_ERROR); } for($i = 0; $i < $file_count; $i++) { $filename = $files['name'][$i]; $tmp = $files['tmp_name'][$i]; $mime_type = get_mime_type($tmp); $uploaded_file = pathinfo($filename, PATHINFO_FILENAME) . '.' . ALLOWED_FILES[$mime_type]; $filepath = UPLOAD_DIR . '/' . $uploaded_file; $success = move_uploaded_file($tmp, $filepath); if(!$success) { $errors[$filename] = "The file $filename was failed to move."; } } $errors ? redirect_with_message(format_messages('The following errors occurred:',$errors), FLASH_ERROR) : redirect_with_message('All files were uploaded successfully.', FLASH_SUCCESS);
- To begin, open the index.html file in your browser.
- You'll see a form with a file upload option. So, click the browse option to find the file you want to upload.
- Note: To select several things, press and hold the ctrl key while selecting each one one at a time.
- After selecting all of the files, click the Upload button.
- The files will be moved to a temporary storage location.
- All file conditions will be checked before the upload.php file is run.
- The file will be uploaded to our directory if all of the prerequisites are met.
- If there are any available, show the success and error messages.
Conclusion :-
This is a simple tutorial on how to use PHP to upload numerous files.
I hope this tutorial on multiple file upload in PHP helps you and you understood how to upload Multiple file in PHP and learned something new.