How to select and upload multiple files with HTML and PHP, using HTTP POST? Last Updated : 07 Aug, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will look at how to upload multiple files with HTML and PHP. Multiple image upload allows the user to select multiple files at once and upload all files to the server. index.html Create a simple HTML page to select multiple files and submit it to upload files on the server. Here, the HTML file contains a form to select and upload files using the POST method. HTML <!DOCTYPE html> <html> <head> <title> Select and upload multiple files to the server </title> </head> <body> <!-- multipart/form-data ensures that form data is going to be encoded as MIME data --> <form action="file_upload.php" method="POST" enctype="multipart/form-data"> <h2>Upload Files</h2> <p> Select files to upload: <!-- name of the input fields are going to be used in our php script--> <input type="file" name="files[]" multiple> <br><br> <input type="submit" name="submit" value="Upload" > </p> </form> </body> </html> file_upload.php The file_upload.php script will handle the file uploading and show the upload status. PHP <?php // Check if form was submitted if(isset($_POST['submit'])) { // Configure upload directory and allowed file types $upload_dir = 'uploads'.DIRECTORY_SEPARATOR; $allowed_types = array('jpg', 'png', 'jpeg', 'gif'); // Define maxsize for files i.e 2MB $maxsize = 2 * 1024 * 1024; // Checks if user sent an empty form if(!empty(array_filter($_FILES['files']['name']))) { // Loop through each file in files[] array 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); // Set upload file path $filepath = $upload_dir.$file_name; // Check file type is allowed or not if(in_array(strtolower($file_ext), $allowed_types)) { // Verify file size - 2MB max if ($file_size > $maxsize) echo "Error: File size is larger than the allowed limit."; // If file with name already exist then append time in // front of name of the file to avoid overwriting of file if(file_exists($filepath)) { $filepath = $upload_dir.time().$file_name; if( move_uploaded_file($file_tmpname, $filepath)) { echo "{$file_name} successfully uploaded <br />"; } else { echo "Error uploading {$file_name} <br />"; } } else { if( move_uploaded_file($file_tmpname, $filepath)) { echo "{$file_name} successfully uploaded <br />"; } else { echo "Error uploading {$file_name} <br />"; } } } else { // If file extension not valid echo "Error uploading {$file_name} "; echo "({$file_ext} file type is not allowed)<br / >"; } } } else { // If no files selected echo "No files selected."; } } ?> Output: Before Uploading the file: After Uploading the file: PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.  Comment More infoAdvertise with us Next Article How to select and upload multiple files with HTML and PHP, using HTTP POST? B Blinkii Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads Upload pdf file to MySQL database for multiple records using PHP We will upload multiple records to the database and display all the records from the database on the same page. In this article, we will see how we can upload PDF files to a MySQL database using PHP. Approach: Make sure you have XAMPP or WAMP installed on your machine. In this tutorial, we will be 7 min read How to Upload Image into Database and Display it using PHP ? Uploading the image/videos into the database and displaying it using PHP is the way of uploading the image into the database and fetching it from the database. Using the PHP code, the user uploads the image or videos they are safely getting entry into the database and the images should be saved into 8 min read How to append data in JSON file through HTML form using PHP ? The purpose of this article is to append data to a JSON file through HTML form using PHP. Approach 1: If the JSON file is not created then we create a new JSON file, send data to it, and append data in it. To see how to create a JSON file by taking data from the HTML form, refer this link. Approach 4 min read How to post data using file_get_contents in PHP ? The file_get_contents() function in PHP is used to read the contents of a file and make HTTP requests using GET and get HTTP responses using POST methods. The HTTP POST request can be made using the $context parameter of the file_get_contents() function, which posts the specified data to the URL spe 3 min read How to move a file into a different folder on the server using PHP? The move_uploaded_file() function and rename() function is used to move a file into a different folder on the server. In this case, we have a file already uploaded in the temp directory of server from where the new directory is assigned by the method. The file temp is fully moved to a new location. 3 min read How to read data from a file stored in XAMPP webserver using PHP ? We have given a file stored on XAMPP server and the task is to read the file from server and display the file content on the screen using PHP. We use some PHP functions to solve this problem. File: A file is set of data stored in a disk in different formats. For example - .txt, .exe, .pdf etc fopen 2 min read How to Get $_POST from multiple check-boxes ? $_POST is an array of variable names. The program given below illustrates how to write HTML structure for multiple valued checkbox and to get the values of multiple valued checkbox using $_POST in PHP. Note: The name attribute of checkboxes must be the same name and must be initialized with an array 2 min read How to send data of HTML form directly to JSON file? To send HTML form data directly to a JSON file. The process of capturing form inputs, converting them to JSON format, and saving them to a file, ensures efficient data handling and storageApproachIn an HTML form containing several fields such as name, college, etc. We want to send the data of our HT 3 min read How to Insert Form Data into Database using PHP ? In modern web development, Handling User Input is a fundamental task. One of the most common Operations is capturing data from a form and inserting it into database. PHP, combined with MySQL, offers a straightforward and powerful way to achieve it.Here, we will see complete process of inserting form 4 min read How to automatically start a download in PHP ? This post deals with creating a start downloading file using PHP. The idea is to make a download button which will redirect you to another page with the PHP script that will automatically start the download. Creating a download button: html <!DOCTYPE html> <html> <head> <meta na 2 min read Like