📖 Scenario: You are building a simple PHP web application that allows users to upload image files. However, you want to make sure the file upload is secure and does not allow harmful files to be uploaded.
🎯 Goal: Create a PHP script that securely handles file uploads by checking the file type and size before saving it to the server.
📋 What You'll Learn
Create an array called allowed_types with the values 'image/jpeg', 'image/png', and 'image/gif'
Create a variable called max_size and set it to 2000000 (2MB)
Write code to check if the uploaded file's MIME type is in allowed_types and its size is less than max_size
If the file passes checks, move it to the uploads/ folder and print 'Upload successful'
If the file fails checks, print 'Upload failed: Invalid file type or size'
💡 Why This Matters
🌍 Real World
Websites often allow users to upload images or documents. Ensuring these files are safe prevents hackers from uploading harmful scripts.
💼 Career
Web developers must secure file uploads to protect websites and users from security risks like malware or unauthorized access.
Progress0 / 4 steps
1
Setup allowed file types
Create an array called allowed_types with these exact values: 'image/jpeg', 'image/png', and 'image/gif'.
PHP
Hint
Use square brackets [] to create the array with the exact strings inside.
2
Set maximum file size
Create a variable called max_size and set it to 2000000 (which means 2 megabytes).
PHP
Hint
Just assign the number 2000000 to the variable $max_size.
3
Check file type and size
Write an if statement that checks if $_FILES['userfile']['type'] is in $allowed_types and $_FILES['userfile']['size'] is less than $max_size.
PHP
Hint
Use in_array() to check if the file type is allowed and compare the size with $max_size.
4
Move file and print result
Inside the if block, move the uploaded file from $_FILES['userfile']['tmp_name'] to 'uploads/' . $_FILES['userfile']['name'] using move_uploaded_file(). Then print 'Upload successful'. In the else block, print 'Upload failed: Invalid file type or size'.
PHP
Hint
Use move_uploaded_file() to save the file and print() to show messages.
Practice
(1/5)
1. Which of the following is the most important reason to validate file types during a PHP file upload?
easy
A. To make the upload process faster
B. To allow uploading any file without restrictions
C. To reduce server storage space
D. To prevent users from uploading harmful scripts disguised as files
Solution
Step 1: Understand file upload risks
Malicious users can upload harmful scripts if file types are not checked.
Step 2: Importance of validation
Validating file types helps block dangerous files and protects the server.
Final Answer:
To prevent users from uploading harmful scripts disguised as files -> Option D
Quick Check:
File type validation = prevent harmful uploads [OK]
Hint: Always check file types to block dangerous uploads [OK]
Common Mistakes:
Ignoring file type validation
Assuming all files are safe
Only checking file extensions without content
2. Which PHP function is commonly used to check the MIME type of an uploaded file for security?
easy
A. mime_content_type()
B. is_uploaded_file()
C. file_get_contents()
D. move_uploaded_file()
Solution
Step 1: Identify function purpose
mime_content_type() returns the MIME type of a file, useful for validation.
5. You want to securely handle file uploads in PHP by allowing only PNG images under 1MB, renaming files to a unique name, and preventing script execution. Which combination of steps is best?
hard
A. Check file size, move file with original name, allow all image types
B. Only check file extension, rename file, save in uploads folder
C. Check MIME type with mime_content_type(), limit size, rename with uniqid(), save outside web root
D. Skip validation, rename file, save in web root
Solution
Step 1: Validate file type and size
Use mime_content_type() to confirm PNG and check size under 1MB.
Step 2: Rename file and secure storage
Rename with uniqid() to avoid conflicts and save outside web root to prevent script execution.
Final Answer:
Check MIME type with mime_content_type(), limit size, rename with uniqid(), save outside web root -> Option C
Quick Check:
Validate type & size + rename + secure storage [OK]
Hint: Validate type/size, rename uniquely, store securely [OK]
Common Mistakes:
Relying only on file extension
Saving files in web root without restrictions
Not renaming files uniquely
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.