All TalkersCode Topics

Follow TalkersCode On Social Media

Multiple File Upload In PHP

Last Updated : Jul 1, 2023

Multiple File Upload In PHP

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>
   <link rel="stylesheet" type="text/css" href="style.css">
   <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">
  1. 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.
  2. 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;
  1. Because the design of the index.html file with CSS is totally up to you, this is an optional step.
  2. 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.
  3. Make a style.css file and save it with the CSS code below.
  4. 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

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.";
  1. 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.
  2. It will initially check to see if the submit button has been touched before moving on to the upload file feature.

Uploading Multiple Files

require_once __DIR__ . '/inc/flash.php';
require_once __DIR__ . '/inc/functions.php';
    '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];
    $filesize = filesize($tmp);
    if ($filesize > MAX_SIZE) {
        $message = sprintf("The file %s is %s which is greater than the allowed size %s",
        $errors[$filesize] = $message;
    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);
  1. To begin, open the index.html file in your browser.
  2. You'll see a form with a file upload option. So, click the browse option to find the file you want to upload.
  3. Note: To select several things, press and hold the ctrl key while selecting each one one at a time.
  4. After selecting all of the files, click the Upload button.
  5. The files will be moved to a temporary storage location.
  6. All file conditions will be checked before the upload.php file is run.
  7. The file will be uploaded to our directory if all of the prerequisites are met.
  8. 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.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪