Hi friends, in this tutorial you will learn how to upload multiple files in PHP together and store them in a folder. Multiple file uploading is a very important part of web development and it is often required while signing up on the website or in the backend of an eCommerce website or any kind of web application. So I will explain the step-by-step process.
Also read, How to Upload Image in PHP and Store in Database and Folder
Required steps to upload multiple files in PHP
Step 1:- First of all, create a PHP file inside the root directory of your local server such as index.php.
Step 2:- Add the below code in the index.php file as shown below
Step 3:- Make sure that you have added enctype=”multipart/form-data” in the form tag and the  in the name attribute of the file which is considered as the array of files.
Step 4:- Open the browser and enter the URL http://localhost/index.php and you can see the image as shown below.
From the above image, you can see that I have created multiple fields with a delete button so that I can add the file as much as I can and also delete it if necessary. You can hit the add file button to create multiple fields.
Creation of multiple fields using jquery
- In index.php, I have added a hidden input field with zero value and a button element with id attribute and an extra div element to show the newly created field.
- Also you can see that a remove() function of jquery is used to delete the newly created fields.
Step 4:- Now create an action file upload.php and add the code as shown below
Explanation of the above code
- Now, we will receive the files with the help of PHP superglobal variable $_FILES as shown below
- Now, we will use the foreach loop to count the files one by one.
- Inside the loop, we will set a destination or storage location where the files will be stored as shown below
- Finally we will move the uploaded files in the destination using the move_uploaded_file() function as shown below.
Note*:- basename() is used to get the filename from the path of the file.
Conclusion:- I hope this tutorial will help you to understand the overview. If there is any doubt then please leave a comment below