Skip to main content

Multiple File Upload with PHP

Sometimes we need to allow our users to upload multiple file upload. On my previous post I was shown the basic of simple file upload with PHP. Today I am going to show you how to allow users to upload multiple files. It is almost similar like simple file upload but we need to do some modification with html markup and php code. multiple attribute is needed to add on the html markup. Major web browsers like Firefox, Chrome, Safari and IE 9+ support this attribute. Now I am going to show you how to upload multiple file with PHP and basic HTML form.


Live Demo Download Source

HTML Markup

Take a look at the html markup bellow. We need to add a simple html form with input type file and submit property. We also need to give file input type file name with box breaks like files[] and need to add a property named multiple. Here accept is an optional property that used to allow users to upload only image files.

<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Multiple File Ppload with PHP</title>
</head>
<body>
  <form action="" method="post" enctype="multipart/form-data">
    <input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
  <input type="submit" value="Upload!" />
</form>
</body>
</html>

PHP Script

This php code handles uploaded files and save to the server.

$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 1024*100; //100 kb
$path = "uploads/"; // Upload directory
$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
 // Loop $_FILES to exeicute all files
 foreach ($_FILES['files']['name'] as $f => $name) {     
     if ($_FILES['files']['error'][$f] == 4) {
         continue; // Skip file if any error found
     }        
     if ($_FILES['files']['error'][$f] == 0) {            
         if ($_FILES['files']['size'][$f] > $max_file_size) {
             $message[] = "$name is too large!.";
             continue; // Skip large files
         }
   elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
    $message[] = "$name is not a valid format";
    continue; // Skip invalid file formats
   }
         else{ // No error found! Move uploaded files 
             if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
             $count++; // Number of successfully uploaded file
         }
     }
 }
}

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery . Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar. We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin . It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.