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.