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" method="post">
    • <input type="file" name="up" required>
    • <input type="submit" value="Upload">
    • </form>
  2. Save the uploaded file in PHP.
    • move_uploaded_file($_FILES["up"]["tmp_name"], $_FILES["up"]["name"]);

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

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

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

 

SIMPLE PHP UPLOAD

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

 

STEP 1) HTML FILE UPLOAD FORM

1-upload.html
<form action="2-upload.php" method="post" enctype="multipart/form-data">
  <input type="file" name="upfile" required>
  <input type="submit" value="Upload">
</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

2-upload.php
<?php
// (A) MOVE UPLOADED FILE TO DESTINATION
$source = $_FILES["upfile"]["tmp_name"];
$destination = $_FILES["upfile"]["name"];
$pass = move_uploaded_file($source, $destination);
 
/* (B) DONE - WHAT'S NEXT?
if ($pass) {
  // REDIRECT TO ANOTHER PAGE?
  header("Location: http://my-site.com/somewhere/");
  exit();
 
  // SHOW AN "UPLOAD OK PAGE"?
  // require "ok.html";
}
 
// (C) DEAL WITH ERRORS?
else {
  // SHOW ERROR MESSAGE - GOOD FOR DEBUGGING
  // BUT NOT ON A LIVE SERVER (SECURITY ISSUES)
  // print_r(error_get_last());
 
  // JUST SHOW GENERIC ERROR MESSAGE?
  exit("UPLOAD ERROR");
} 
*/

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).

 

 

EXTRAS

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

 

PHP.INI FILE UPLOAD 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.

 

 

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!

4 thoughts on “Very Simple PHP File Upload (A Quick Example)”

  1. Hi,

    I really like using this script.
    Also, would you have a code that would add how much time is left until the upload is complete?

Leave a Comment

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