Upload Multiple Files In PHP (Very Simple Example)

Welcome to a quick tutorial on how to upload multiple files in PHP. So you want to create an upload form that accepts multiple files at once?

To upload multiple files in PHP:

  1. Set the HTML form to accept multiple files.
    • <form action="UP.PHP" enctype="multipart/form-data">
    • <input type="file" name="up[]" multiple required>
    • <input type="submit" value="Upload File">
    • </form>
  2. Loop through the uploaded files in PHP and save them.
    • for ($i=0; $i<count($_FILES["up"]["name"]); $i++) {
    • move_uploaded_files($_FILES["up"]["tmp_name"][$i], $FILES["up"]["name"][$i]);
    • }

That covers the quick basics, but read on for a detailed example!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

PHP UPLOAD MULTIPLE FILES

All right, let us now get into an example of uploading multiple files in PHP.

 

PART 1) HTML UPLOAD FORM

1-upload.html
<form action="2-upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="upfile[]" multiple required>
  <input type="submit" value="Upload File">
</form>

This is “just a normal HTML upload form”. But as in the introduction, we need to set this to accept multiple files:

  • name="upfile[]" The uploaded files will be accepted as an array in PHP $_FILES["upfile"].
  • multiple Self-explanatory. Sets the file input field to accept multiple files.

 

PART 2) PHP UPLOAD HANDLER

2-upload.php
<?php
// (A) SOME FLAGS
$total = isset($_FILES["upfile"]) ? count($_FILES["upfile"]["name"]) : 0 ;
$status = [];
 
// (B) PROCESS FILE UPLOAD
if ($total>0) { for ($i=0; $i<$total; $i++) {
  $source = $_FILES["upfile"]["tmp_name"][$i];
  $destination = $_FILES["upfile"]["name"][$i];
  if (move_uploaded_file($source, $destination) === false) {
    $status[] = "Error uploading to $destination";
  }
}} else { $status[] = "No files uploaded!"; }
 
/* (C) DONE - WHAT'S NEXT?
if (count($status)==0) {
  // REDIRECT TO OK PAGE?
  header("Location: http://site.com/somewhere/");
 
  // SHOW AN "OK" PAGE?
  require "OK.PHP"
}
 
// (D) HANDLE ERRORS?
else {
  // (D1) SHOW ERRORS?
  // print_r($status);
 
  // (D2) REDIRECT BACK TO UPLOAD PAGE?
  header("Location: http://site.com/1-upload.html/?error=1");
}

Keep calm and look carefully. This is essentially the same as the introduction snippet, looping through the list of uploaded files and saving them on the server. It just has a couple more checks, so you can handle errors or use them for debugging if you want.

 

 

EXTRAS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

UPLOAD LIMITS

php.ini
post_max_size = 10M
upload_max_filesize = 10M

Please take note that the upload file size limits still apply in multiple uploads. While this basic uploader works, it is not the best if you want to allow massive uploads – Check out the drag-and-drop and large file uploads tutorials below.

 

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *