PHP File Upload System
# Chapter 24: PHP File Upload System
1. Introduction
Welcome to Chapter 24! A modern web application allows users to upload content—profile pictures, PDF resumes, or video files. Handling file uploads in PHP is a critical skill, but it is also one of the most dangerous. If you allow users to upload files without strict validation, a hacker could upload a malicious.php script and take over your entire server. In this chapter, we will learn how to process file uploads using the $_FILES superglobal, validate file types and sizes securely, and save them to a designated folder.
2. Learning Objectives
By the end of this chapter, you will be able to:- Structure an HTML form to accept file uploads.
-
Understand the
$_FILESsuperglobal array structure.
- Move an uploaded file from a temporary folder to a permanent folder.
- Validate file extensions (e.g., only allow .jpg or .png).
- Validate file sizes.
- Handle upload errors gracefully.
3. The Upload Form
To upload files, your HTML<form> must use method="POST" and it must include the enctype="multipart/form-data" attribute. Without enctype, the file data will simply not be sent to the server.
4. Understanding $_FILES
When the form is submitted, PHP temporarily saves the file in a hidden server directory and populates the $_FILES array. If your input name is profile_pic, $_FILES['profile_pic'] will contain an associative array with 5 pieces of information:
-
name: The original name of the file (e.g.,vacation.jpg).
-
type: The MIME type of the file (e.g.,image/jpeg).
-
tmp_name: The temporary file path on the server (e.g.,C:\xampp\tmp\phpABCD.tmp).
-
error: An error code (0 means success).
-
size: The size of the file in bytes.
5. Basic Upload Processing (The Unsafe Way)
To save the file permanently, we must use themove_uploaded_file() function to move it from its temporary location (tmp_name) to a folder we control (like an uploads/ folder).
*Note: You must manually create the uploads folder in your project directory before running this!*
6. The Danger: Why Validation is Mandatory
If a hacker uploads a file namedhack.php containing malicious code, the basic script above will gladly save it to uploads/hack.php. The hacker can then visit yourwebsite.com/uploads/hack.php in their browser, executing the code and taking control of your server.
We MUST strictly validate the file extension!
7. Secure Validation Strategy
We will validate three things:-
1.
Ensure the file has no errors (
error == 0).
- 2. Ensure the file is not too large (e.g., < 2MB).
-
3.
Ensure the file extension is strictly allowed (e.g., only
jpg,png).
8. Output Explanations
In the secure script,pathinfo() extracts just the extension (.jpg). We check if that extension exists inside our $allowed_extensions array using in_array(). If someone tries to upload .php, the script dies immediately.
Furthermore, we use uniqid() to generate a completely random string for the new filename (e.g., img_60f3b4c123.jpg). This ensures that if two users upload an image named avatar.jpg, the second one doesn't overwrite the first one on the server!
9. Common Mistakes
-
Forgetting
enctype="multipart/form-data": The form will submit, but$_FILESwill be empty.
-
Trusting the
typearray key:$_FILES['profile_pic']['type']is sent by the browser. A hacker can easily fake this to sayimage/jpegwhile uploading a.phpfile. Never rely on the MIME type alone for security; always verify the extension usingpathinfo().
-
Directory Permissions: On live Linux servers, PHP might not have permission to write to your
uploads/folder. You may need to CHMOD the folder to 755 or 775.
10. Best Practices
-
Never keep the original uploaded filename. Always generate a random, unique name using
uniqid().
-
Never store images in the database. Store the file in a folder, and save the *string path* (e.g.,
uploads/img_123.jpg) in the database.
-
Disable PHP execution in your
uploads/directory via server configuration (.htaccess) as an ultimate fallback security measure.
11. Exercises
-
1.
Create a folder named
uploadsin your root directory.
- 2. Build the secure upload form and PHP script from Section 7.
-
3.
Attempt to upload a
.txtfile and verify the security blocks it.
-
4.
Upload a
.jpgand verify it appears in theuploadsfolder with a unique name.
12. Mini Project: Profile Image Uploader
Task: Build a page that allows a user to upload an image. Once uploaded securely, save the path to a session, and immediately display the uploaded image dynamically on the page using an<img> tag.
13. Coding Challenges
Challenge 1: Modify the validation script. Add a check to ensure the file is an actual image, not a fake file renamed to.jpg. Research and use the PHP getimagesize() function to accomplish this.
14. MCQs with Answers
1. What attribute is strictly required on an HTML form to upload files? A)method="FILE"
B) upload="enabled"
C) enctype="multipart/form-data"
D) data-type="file"
*Answer: C*
2. Where does PHP store an uploaded file immediately after submission?
A) In the database.
B) In a temporary system directory defined by the server.
C) In the uploads folder.
D) In the $_SESSION array.
*Answer: B*
3. Why should you generate a random unique name for uploaded files? A) To make the file download faster. B) To encrypt the image. C) To prevent users from guessing file URLs and to prevent overwriting files that happen to have the same original name. D) Because PHP cannot read original filenames. *Answer: C*
15. Interview Questions
Q: Explain the process of securely handling a file upload in PHP. *A:* First, ensure the form hasenctype="multipart/form-data". Upon submission, check $_FILES['file']['error'] for success. Extract the file extension using pathinfo() and check it against a strict whitelist array (e.g., ['jpg', 'png']). Validate the file size. If all checks pass, generate a secure, unique filename using uniqid(), and use move_uploaded_file() to transfer it from the temporary directory to the final destination.
Q: Should you store images as BLOBs in the database, or in a folder?
*A:* While databases support BLOB (Binary Large Object) data, it is almost universally considered best practice to store files in a folder on the server's hard drive. Storing large files in the database massively bloats the database size, slows down regular text queries, and makes backups extremely difficult. Store the file in a folder, and store the text path (/uploads/file.jpg) in the database.
16. FAQs
Q: My upload script works for small images, but fails silently for large 5MB images. Why? *A:* PHP has a built-in configuration limit for file uploads (usually set to 2MB by default inphp.ini). If a file exceeds upload_max_filesize or post_max_size, PHP drops the file before your script even runs. You must edit your server's php.ini file to increase these limits.
17. Summary
You are now capable of handling complex multimedia data! You learned how to configure HTML forms for file transfer, navigate the multidimensional$_FILES array, securely validate file extensions and sizes, generate collision-proof filenames, and move temporary files into permanent storage directories safely.