Bird
Raised Fist0
PHPprogramming~20 mins

File upload security risks in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Upload Security Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code handling file uploads?
Consider this PHP snippet that checks the uploaded file type before saving it. What will it output if a user uploads a file named image.php with MIME type image/jpeg?
PHP
<?php
$allowed_types = ['image/jpeg', 'image/png'];
if (in_array($_FILES['file']['type'], $allowed_types)) {
    echo "Upload allowed";
} else {
    echo "Upload denied";
}
?>
ASyntax error
BUpload allowed
CUndefined index error
DUpload denied
Attempts:
2 left
💡 Hint
The code checks only the MIME type, not the file extension.
🧠 Conceptual
intermediate
1:30remaining
Which risk is NOT typically associated with file uploads?
Select the option that is NOT a common security risk when handling file uploads in PHP.
AOverwriting existing files on the server
BDenial of Service by uploading very large files
CUploading a malicious script that executes on the server
DSQL Injection through file content
Attempts:
2 left
💡 Hint
Think about how SQL Injection usually happens.
🔧 Debug
advanced
2:30remaining
Why does this PHP file upload code allow dangerous files?
This PHP code tries to restrict uploads to images only. Why does it still allow uploading a PHP script named shell.jpg?
PHP
<?php
$allowed_ext = ['jpg', 'jpeg', 'png'];
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if (in_array($ext, $allowed_ext)) {
    move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $filename);
    echo "File uploaded";
} else {
    echo "Invalid file type";
}
?>
AThe code does not check the MIME type, so a PHP file renamed with .jpg extension passes
BThe code uses pathinfo incorrectly causing it to always return empty extension
CThe $_FILES array is empty, so the check is skipped
DThe move_uploaded_file function is disabled on the server
Attempts:
2 left
💡 Hint
Check how the extension is verified and what happens if a file is renamed.
📝 Syntax
advanced
1:30remaining
What error does this PHP file upload validation code produce?
Identify the error in this PHP snippet that tries to validate file size and type.
PHP
<?php
if ($_FILES['file']['size'] > 2000000 && $_FILES['file']['type'] == 'image/png') {
    echo "File accepted";
} else {
    echo "File rejected";
}
?>
AUndefined index error if no file uploaded
BThe condition wrongly uses && instead of || causing wrong logic
CNo error; code runs and outputs 'File rejected' for large PNG files
DSyntax error due to missing semicolon
Attempts:
2 left
💡 Hint
What happens if the 'file' key is missing in the $_FILES array?
🚀 Application
expert
3:00remaining
How many files will be accepted by this PHP upload filter?
Given this PHP code that filters uploaded files, how many files from the list below will be accepted?
PHP
<?php
$allowed_types = ['image/jpeg', 'image/png'];
$allowed_ext = ['jpg', 'jpeg', 'png'];
function is_file_allowed($file) {
    $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
    if (in_array($file['type'], $GLOBALS['allowed_types']) && in_array($ext, $GLOBALS['allowed_ext'])) {
        return true;
    }
    return false;
}

$files = [
    ['name' => 'photo.jpg', 'type' => 'image/jpeg'],
    ['name' => 'script.php', 'type' => 'image/jpeg'],
    ['name' => 'image.png', 'type' => 'image/png'],
    ['name' => 'document.jpg', 'type' => 'application/pdf']
];

$count = 0;
foreach ($files as $file) {
    if (is_file_allowed($file)) {
        $count++;
    }
}
echo $count;
?>
A3
B1
C2
D4
Attempts:
2 left
💡 Hint
Check both MIME type and extension for each file.

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

  1. Step 1: Understand file upload risks

    Malicious users can upload harmful scripts if file types are not checked.
  2. Step 2: Importance of validation

    Validating file types helps block dangerous files and protects the server.
  3. Final Answer:

    To prevent users from uploading harmful scripts disguised as files -> Option D
  4. 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

  1. Step 1: Identify function purpose

    mime_content_type() returns the MIME type of a file, useful for validation.
  2. Step 2: Compare with other functions

    move_uploaded_file() moves files, file_get_contents() reads content, is_uploaded_file() checks upload status.
  3. Final Answer:

    mime_content_type() -> Option A
  4. Quick Check:

    Check MIME type = mime_content_type() [OK]
Hint: Use mime_content_type() to verify file type safely [OK]
Common Mistakes:
  • Using move_uploaded_file() to check file type
  • Confusing file_get_contents() with type checking
  • Not verifying MIME type at all
3. What will be the output of this PHP code snippet if a user uploads a file named 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';
}
medium
A. File too large
B. Upload successful
C. No output
D. Syntax error

Solution

  1. Step 1: Understand size check logic

    The code compares uploaded file size to maxSize (2MB).
  2. Step 2: Compare actual file size

    Uploaded file is 5MB, which is greater than 2MB, so condition is true.
  3. Final Answer:

    File too large -> Option A
  4. Quick Check:

    5MB > 2MB triggers 'File too large' [OK]
Hint: Compare file size with max allowed size [OK]
Common Mistakes:
  • Confusing bytes and megabytes
  • Not multiplying correctly for max size
  • Ignoring file size check
4. Identify the security issue in this PHP file upload code snippet:
if (move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name'])) {
    echo 'Upload successful';
} else {
    echo 'Upload failed';
}
medium
A. Using original filename without sanitization
B. All of the above
C. Missing check if file is uploaded via HTTP POST
D. No file size check before upload

Solution

  1. Step 1: Check file size validation

    Code does not check file size before moving the file.
  2. Step 2: Check filename handling

    Using original filename directly risks overwriting and security issues.
  3. Step 3: Check upload method validation

    Missing is_uploaded_file() check to confirm HTTP POST upload.
  4. Final Answer:

    All of the above -> Option B
  5. Quick Check:

    Missing size, sanitization, and upload checks [OK]
Hint: Check size, sanitize name, verify upload method [OK]
Common Mistakes:
  • Ignoring filename sanitization
  • Not verifying upload method
  • Skipping file size 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

  1. Step 1: Validate file type and size

    Use mime_content_type() to confirm PNG and check size under 1MB.
  2. Step 2: Rename file and secure storage

    Rename with uniqid() to avoid conflicts and save outside web root to prevent script execution.
  3. Final Answer:

    Check MIME type with mime_content_type(), limit size, rename with uniqid(), save outside web root -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard