0
0
PHPprogramming~20 mins

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

Choose your learning style9 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.