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!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Upload Many Useful Bits & Links
The End

 

DOWNLOAD & NOTES

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

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

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://my-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://my-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.

 

 

USEFUL BITS & LINKS

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

 

INFOGRAPHIC CHEAT SHEET

Upload Multiple Files In PHP (click to enlarge)

 

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.