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.

 

 

TLDR – QUICK SLIDES

 

TABLE OF CONTENTS

 

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

 

 

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

 

 

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

 

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

 

INFOGRAPHIC CHEAT SHEET

Simple PHP File Upload (Click to Enlarge)

 

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!

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

Leave a Comment

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