Upload Multiple Files In PHP

Upload Multiple Files In PHP
Written by ProgrammingPot

After learning how to find Free Blog Post image, In this article, we will learn how Upload Multiple Files In PHP. I have combined both these features into one i.e Uploading multiple files using php (of course you can filter images, documents based on your requirement). Also included is the trick to extend the file upload size so the user can upload files larger than 128M/2M (local/online server) which is set by default.

Upload Multiple Files In PHP

Scope of this script?

Using this script user can upload one or multiple files. Extend the upload limit, by default the maximum upload size limit, is 128mb on local server and 2mb/8mb on the online server (it may vary from server to server).

Method 1: File field with multiple attribute tag

This is the first technique for Upload Multiple Files In PHP, it’s very easy and simple. What you have to do is just set up a form and add a file field, add this attribute to that file field (multiple=”multiple”). Also, add an array symbol with the file name, make sure you have a submit button and that’s it. Rest of the work will be done by the PHP script. Whenever you attach a file you can now select multiple files and attach to it.

<form name="f" action="index.php" method="post" enctype="multipart/form-data">
  <div style="width: 380px; margin: 0 auto;">
     Attach multiple Files:
     <input class="files" name="user_files[]" type="file" multiple="multiple" >
     <div class="height10"></div>
     <div><input type="submit" class="submit" name="sub" /> </div>

Method 2: Multiple file field with jquery

This is the second method for Upload Multiple Files In PHP. In this method you can add/remove as many file fields with jquery, just make sure that every field name must be same and have an array symbol with it i.e closed with double square bracket […].

<input class="files" name="user_files[]" type="file" >
<span><a href="javascript:void(0);" class="add" >Add More</a></span>
<!-- In this container dynamic fields will display -->
<div class="contents"></div>
<script src="jquery-1.9.0.min.js"></script>
$(document).ready(function() {
  $(".add").click(function() {
    $('<div><input class="files" name="user_files[]" type="file" ><span class="rem" ><a href="javascript:void(0);" >Remove</span></div>').appendTo(".contents");
  $('.contents').on('click', '.rem', function() {

Uploading the files with php

Now that you are done setting up with form move on to the final step i.e writing the PHP script that will be executed when the form is submitted. Have in mind that the file is an array ( $_FILE[“file_name”] ) and since you are uploading multiple files so now its an array of arrays.

if (count($_FILES["user_files"]) > 0) {
  $folderName = "uploads/";
  $counter = 0;
  for ($i = 0; $i < count($_FILES["user_files"]["name"]); $i++) {
    if ($_FILES["user_files"]["name"][$i] <> "") {
      $ext = strtolower(end(explode(".", $_FILES["user_files"]["name"][$i])));
      $filePath = $folderName . rand(10000, 990000) . '_' . time() . '.' . $ext;
      if (!move_uploaded_file($_FILES["user_files"]["tmp_name"][$i], $filePath)) {
         $msg .= "Failed to upload" . $_FILES["user_files"]["name"][$i] . ". <br>";
    $msg = ($counter == 0) ? "Files uploaded Successfully" : "Erros : ".$msg;

In case you have to upload larger files try changing these from your php.ini file(if you have access to it)

make them equal size
post_max_size = 128M
upload_max_filesize = 128M

In case you don’t have access to php.ini file try writing these two lines at the top of your page.

ini_set('post_max_size', '128M');
ini_set('upload_max_filesize', '128M');


Leave a Comment