Very Simple PHP File Upload – A Quick Example

Welcome to a quick tutorial and simple example of how to handle a file upload in PHP. Some beginners will probably freak out on hearing “file upload”, but it is actually a very simple process.

To handle file uploads in PHP, we only need to create a simple HTML form and save the upload in PHP:

  1. Create an HTML upload form.
    • <form action="UPLOAD.PHP" enctype="multipart/form-data">
    • <input type="file" name="upfile" required>
    • <input type="submit" value="Upload">
    • </form>
  2. Save the uploaded file in PHP.
    • move_uploaded_file($_FILES['upfile']['tmp_name'], $_FILES["upfile"]["name"]);

That’s all for the basics, but let us walk through more examples in this guide – Read on!

ⓘ I have included a zip file with all the example 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.

 

 

REAL QUICK TUTORIAL

 

TABLE OF CONTENTS

Download & Notes PHP File Upload Useful Bits & Links
The End

 

DOWNLOAD & NOTES

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

 

EXAMPLE CODE DOWNLOAD

Click here to download the example code in a zip file – I have released it under the MIT License, so feel free to build on top of it if you want to.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer 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.

 

PHP.INI SETTINGS

If you are having trouble with the file uploads, please ensure that uploads are enabled on your server. Just open up the php.ini file and check the following:

php.ini
file_uploads = On
post_max_size = 10M
upload_max_filesize = 10M
  • Make sure that the file_uploads is set to On, or all your uploads will fail no matter what.
  • Set the post_max_size and upload_max_filesize to a reasonable size.

 

 

SIMPLE PHP UPLOAD

All right, let us now start with a simple upload handler – Which is very straightforward and not cause any brain damage.

 

STEP 1) HTML FILE UPLOAD FORM

1a-upload.html
<form action="1b-upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="upfile" required>
  <input type="submit" value="Upload" name="submit">
</form>

That’s right, this is just a “normal” HTML form with an <input type="file"/> field. Also, take note of enctype="multipart/form-data" – This is required for file uploads.

 

STEP 2) PHP FILE UPLOAD HANDLER

1b-upload.php
<?php
// (A) MOVE UPLOADED FILE TO DESTINATION
$source = $_FILES["upfile"]["tmp_name"];
$destination = $_FILES["upfile"]["name"];
move_uploaded_file($source, $destination);
 
// (B) DONE - WHAT'S NEXT?
// REDIRECT TO ANOTHER PAGE?
// header('Location: http://my-site.com/somewhere/');
// SHOW AN "UPLOAD OK PAGE"?
// require "ok.html";

What is happening here is that PHP stores all uploaded files into a temporary folder by default, as set in upload_tmp_dir of the php.ini file. All we need to do is to move the uploaded file out of the temporary folder – move_uploaded_file(SOURCE, DESTINATION).

 

 

EXTRA) UPLOAD MULTIPLE & FILE TYPE RESTRICTION

THE HTML

2a-more-upload.html
<form action="2b-more-upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="upfile[]" multiple="multiple" accept="image/*">
  <input type="submit" value="Upload File" name="submit">
</form>

So what if we want to allow the user to upload multiple files at once? Restrict to image files only?

  • Add a multiple="multiple" property on the <input type="file"> field.
  • Append a pair of square brackets on the name – name="upfile[]".
  • To restrict the allowed file type, set an accept attribute – accept="image/*". Check out MDN for more on the file types.

THE PHP

2b-more-upload.php
<?php
for ($i=0; $i<count($_FILES['upfile']['name']); $i++) {
  $source = $_FILES["upfile"]["tmp_name"][$i];
  $destination = $_FILES["upfile"]["name"][$i];
  move_uploaded_file($source, $destination);
}
  • Remember that the corresponding uploads can be found in $_FILES['upfile']?
  • It’s still the same here, except that $_FILES['upfile'] is now an array with multiple files.
  • Simply use a for() loop to run through the uploaded files, do the same – Move the uploaded files out of the temporary folder.

 

 

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.

 

INFOGRAPHIC CHEAT SHEET

Simple PHP File Upload (Click to Enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end of this tutorial. I hope it has helped to solve your upload woes, and if you have anything to add to this guide, please feel free to comment below. Good luck and happy coding!

1 thought on “Very Simple PHP File Upload – A Quick Example”

Leave a Comment

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