Uploading files can let users add content to your website. But if not careful, bad files can harm your site or steal data.
File upload security risks in PHP
<?php // Example of checking file upload if (isset($_FILES['upload'])) { $file = $_FILES['upload']; // Check file type and size here } ?>
Always check the file type and size before saving.
Never trust the file name or MIME type sent by the browser.
<?php // Check file extension $allowed = ['jpg', 'png', 'gif']; $ext = pathinfo($_FILES['upload']['name'], PATHINFO_EXTENSION); if (!in_array(strtolower($ext), $allowed)) { echo 'File type not allowed.'; } ?>
<?php // Check file size (max 2MB) if ($_FILES['upload']['size'] > 2 * 1024 * 1024) { echo 'File is too large.'; } ?>
<?php // Move uploaded file safely $target = 'uploads/' . basename($_FILES['upload']['name']); if (move_uploaded_file($_FILES['upload']['tmp_name'], $target)) { echo 'File uploaded successfully.'; } else { echo 'Upload failed.'; } ?>
This program checks the file type and size, then saves the file with a unique name to avoid overwriting. It shows clear messages for success or errors.
<?php if (isset($_FILES['upload'])) { $allowed = ['jpg', 'png', 'gif']; $ext = strtolower(pathinfo($_FILES['upload']['name'], PATHINFO_EXTENSION)); if (!in_array($ext, $allowed)) { echo 'Error: Only JPG, PNG, GIF files allowed.'; exit; } if ($_FILES['upload']['size'] > 2 * 1024 * 1024) { echo 'Error: File size must be under 2MB.'; exit; } $target_dir = 'uploads/'; $target_file = $target_dir . uniqid('img_', true) . '.' . $ext; if (move_uploaded_file($_FILES['upload']['tmp_name'], $target_file)) { echo 'Success: File uploaded as ' . basename($target_file); } else { echo 'Error: Upload failed.'; } } else { echo 'No file uploaded.'; } ?>
Always rename uploaded files to avoid overwriting and hiding original names.
Store uploaded files outside the web root or restrict access to prevent direct execution.
Scan files for viruses or malware if possible before saving.
File uploads can be risky if you don't check the file type and size.
Always validate and rename files before saving to keep your site safe.
Use clear error messages to help users upload correct files.
Practice
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 DQuick Check:
File type validation = prevent harmful uploads [OK]
- Ignoring file type validation
- Assuming all files are safe
- Only checking file extensions without content
Solution
Step 1: Identify function purpose
mime_content_type() returns the MIME type of a file, useful for validation.Step 2: Compare with other functions
move_uploaded_file() moves files, file_get_contents() reads content, is_uploaded_file() checks upload status.Final Answer:
mime_content_type() -> Option AQuick Check:
Check MIME type = mime_content_type() [OK]
- Using move_uploaded_file() to check file type
- Confusing file_get_contents() with type checking
- Not verifying MIME type at all
image.jpg with size 5MB, but the allowed max size is 2MB?$maxSize = 2 * 1024 * 1024; // 2MB
if ($_FILES['file']['size'] > $maxSize) {
echo 'File too large';
} else {
echo 'Upload successful';
}Solution
Step 1: Understand size check logic
The code compares uploaded file size to maxSize (2MB).Step 2: Compare actual file size
Uploaded file is 5MB, which is greater than 2MB, so condition is true.Final Answer:
File too large -> Option AQuick Check:
5MB > 2MB triggers 'File too large' [OK]
- Confusing bytes and megabytes
- Not multiplying correctly for max size
- Ignoring file size check
if (move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name'])) {
echo 'Upload successful';
} else {
echo 'Upload failed';
}Solution
Step 1: Check file size validation
Code does not check file size before moving the file.Step 2: Check filename handling
Using original filename directly risks overwriting and security issues.Step 3: Check upload method validation
Missing is_uploaded_file() check to confirm HTTP POST upload.Final Answer:
All of the above -> Option BQuick Check:
Missing size, sanitization, and upload checks [OK]
- Ignoring filename sanitization
- Not verifying upload method
- Skipping file size validation
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 CQuick Check:
Validate type & size + rename + secure storage [OK]
- Relying only on file extension
- Saving files in web root without restrictions
- Not renaming files uniquely
Ready to prove it?
Pass the test and this pattern moves unknown
