Whether you’re creating a file-sharing platform, a social media website, or any other type of web application, the ability to upload multiple files at once can significantly improve user experience.
In PHP, it is possible to upload multiple files using a single input file element. You just need to customize your single file upload PHP code and enable your file element to select multiple files.
In this tutorial, I show you how to implement multiple files upload with validation in PHP.
Contents
- HTML – Create Multiple File upload <form >
- PHP – Upload Multiple selected files
- Completed Code
- Demo
- Conclusion
1. HTML – Create Multiple File upload <form >
Here, are the steps to enable multiple file uploads in <form>.
Steps
- First, create a
<form>
and addenctype='multiple/form-data'
attribute.
<form method='post' action='' enctype='multipart/form-data'>
- Create an input
type='file'
element and for enabling multiple files selection addmultiple
attribute. For reading all selected files when<form>
submitted add [] brackets at the end of a name that denotes an Array.
<input type="file" name="file[]" id="file" multiple>
- Add a submit button.
Completed Code
<form method='post' action='' enctype='multipart/form-data'> <input type="file" name="file[]" id="file" multiple> <input type='submit' name='submit' value='Upload'> </form>
2. PHP – Upload Multiple selected files
When <form >
submitted then first count selected files and loop through all files. Select files on the bases of an index.
I am storing files to uploads
folder. Assign file name with upload path to $location
, assign file extension to $extension
.
Initialize $valid_extensions
Array with allowed file upload extensions. If extension exists in $valid_extensions
Array then upload the file.
Completed Code
<?php if(isset($_POST['submit'])){ $countfiles = count($_FILES['file']['name']); $totalFileUploaded = 0; for($i=0;$i<$countfiles;$i++){ $filename = $_FILES['file']['name'][$i]; ## Location $location = "uploads/".$filename; $extension = pathinfo($location,PATHINFO_EXTENSION); $extension = strtolower($extension); ## File upload allowed extensions $valid_extensions = array("jpg","jpeg","png","pdf","docx"); $response = 0; ## Check file extension if(in_array(strtolower($extension), $valid_extensions)) { ## Upload file if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$location)){ echo "file name : ".$filename."<br/>"; $totalFileUploaded++; } } } echo "Total File uploaded : ".$totalFileUploaded; }
3. Completed Code
<!doctype html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Multiple files upload at once with php</title> </head> <body > <span style='color: red;margin-bottom:8px; margin-top:10px;'>NOTE - Files not upload to the server only displaying names of file. </span><br/> <form method='post' action='' enctype='multipart/form-data'> <input type="file" name="file[]" id="file" multiple><br/><br/> <input type='submit' name='submit' value='Upload'> </form> <?php if(isset($_POST['submit'])){ $countfiles = count($_FILES['file']['name']); $totalFileUploaded = 0; for($i=0;$i<$countfiles;$i++){ $filename = $_FILES['file']['name'][$i]; ## Location $location = "uploads/".$filename; $extension = pathinfo($location,PATHINFO_EXTENSION); $extension = strtolower($extension); ## File upload allowed extensions $valid_extensions = array("jpg","jpeg","png","pdf","docx"); $response = 0; ## Check file extension if(in_array(strtolower($extension), $valid_extensions)) { ## Upload file if(move_uploaded_file($_FILES['file']['tmp_name'][$i],$location)){ echo "file name : ".$filename."<br/>"; $totalFileUploaded++; } } } echo "Total File uploaded : ".$totalFileUploaded; } ?> </body> </html>
4. Demo
5. Conclusion
You have to add []
with the file name and multiple
attribute in the HTML and access the selected file using the index position and perform file upload operation using PHP.
If you are using more than one file element for a single task then you can use the above PHP script as a replacement.
Make sure to check upload_max_filesize
and post_max_size
values in the php.ini file if you are allowing large files to upload.
Nice one, but for me it is only uploading 3 files at one time. When I look at the code I don’t see any such restrictions. any idea why?
I think that your multiple selected files exceeding the set max size which you can increase my changing upload_max_filesize and post_max_size values in php.ini according to your need. After changing values restart your server.
Very nice article on multi image upload. I have tried it and worked flawlessly.
Dear Singh,how to make for each customer have their own folder for upload,..e.g..new customer-new name-new folder for images , et cetera….Regards Zex
For this, you need to dynamically create the folder using mkdir() method according to the name of the user and select it for upload.
I want to three uploads button and a submit button to click and temporarily store the file in the server and then save the the file in a folder, then the path in a database.
Please help me, how do i go by that
Hi Mawuli,
I already wrote a tutorial on Upload and store an image in the Database with PHP which you can view to know the file path storing in the database.
Very helpful , thank you
Hello!
Thank you so much for sharing the full script with us. It works!
Could I ask you a favour? I am searching for a script which would allow to upload multiple files on a FTP server without using cURL. Have found this – https://stackoverflow.com/questions/22538240/upload-files-to-remote-server-using-php – but I merge it with yours.
I would be grateful for your help, thanks a lot!
How to include the file type, size and error checks to your code?
I want if all files are from allowed extension then upload it otherwise no files shall be uploaded even if some of the files are from allowed extension
Hi Adnan,
For this, you need to create a boolean variable and assign true to it. Loop on all files and check for an extension. If an extension is not valid then assign false to a boolean variable and break the loop.
If a boolean variable has true value then again loop on the files and upload it.
I can get this to work great while it stands alone. I can upload files to the folder i designate on my server.
my issue begins when I add this to my already existing form that emails the information back to me once it is filled out. I want to have the option to upload files (photos) and and continue sending me the email as usual. i think it has to do with the fact that it already has a command to email me when the ‘submit’ is clicked. I am a complete novice and am struggling with this.
Hi Woody,
You can use AJAX for file uploading.
Thanks for sharing.
Can you please help me?
This file just allow single file at a time can you make change for multiple upload?
I not understand where I make the change.
https://drive.google.com/file/d/1FzlQe9doEWLlSPlWarp4M0524OesNFU6/view?usp=sharing
Thanks.
Please help me dear Yogesh Singh.
your code is working. thank you soo much brother.
You’re welcome.
Hi Jogesh, super helpful peace of content. Just what I needed.Thanks a lot. It helped me to write a cool update for my own CMS.
Kind regards, Guido
I tried this code for uploading multiple images using camera.
I add capture =”camera” in the input field.
But it works for a single image
I want to use your code for multiple images.
Help me.
Hi Yash,
Are you able to select multiple files? and added validation for the image file?
How tabel structure goes to multiple upload?
Hi Thushi,
You can view the following tutorial for reference.
Hi, how can I move files into phpmyadmin and display it on index pag ? I need to do a document management system for school and i need help . plsss
Hi Mihaela,
You can view following tutorial for reference.
I have searched many articles but not found 3 (upload inputs) different file upload by checking file size and type validation in php mysql
$countfiles = count($_FILES[‘file’][‘name’]);
This only counts to 20…
Even if you put more files and it shows 21 files but it only uploads 20
And i have put allowed file size to 1000000000
Great script. If anyone want to check for file size and extension, use following conditions:
$file_size = $_FILES[‘file’][‘size’];
$file_type = pathinfo($file, PATHINFO_EXTENSION);
Excellent post! Thanks for the content.It saves my time. Thanks a lot.
Hi Brother its only uploading the last file i selected what should i do
Hi, nice job, it’s working great !
But i would like to upload multiple file in a select diretory folder. I tried to mix your code with this one that allow to retrieve the list of directories and display it in a select box to users : https://stackoverflow.com/questions/60912432/upload-multiple-files-images-and-select-the-directory-folder-php
But i didn’t succeed to make the two code working together.
Can you help ?
Hi.
How can I upload files to current path? (without upload folder)
Thanks