0
0
PHPprogramming~20 mins

$_FILES for file uploads in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Upload 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 a file upload?

Consider the following PHP script that processes a file upload. What will be the output if a file named example.txt of size 1024 bytes is uploaded successfully?

PHP
<?php
if ($_FILES['upload']['error'] === UPLOAD_ERR_OK) {
    echo "File name: " . $_FILES['upload']['name'] . "\n";
    echo "File size: " . $_FILES['upload']['size'] . " bytes\n";
} else {
    echo "Upload failed with error code: " . $_FILES['upload']['error'];
}
?>
AUpload failed with error code: 1
B
File name: example.txt
File size: 1024 bytes
C
File name: 
File size: 0 bytes
DUpload failed with error code: 4
Attempts:
2 left
💡 Hint

Check the error value in $_FILES to confirm if the upload was successful.

🧠 Conceptual
intermediate
1:00remaining
Which $_FILES key holds the temporary file path?

When a file is uploaded via a form in PHP, which key in the $_FILES array contains the temporary file path on the server?

Atmp_name
Berror
Cname
Dsize
Attempts:
2 left
💡 Hint

The temporary file path is where PHP stores the uploaded file before you move it.

🔧 Debug
advanced
2:30remaining
Why does this PHP file upload code fail to move the uploaded file?

Examine the code below. It tries to move an uploaded file but fails. What is the cause?

PHP
<?php
if (move_uploaded_file($_FILES['file']['name'], '/uploads/' . $_FILES['file']['name'])) {
    echo "Upload successful.";
} else {
    echo "Upload failed.";
}
?>
AUsing $_FILES['file']['name'] as source instead of $_FILES['file']['tmp_name']
BThe destination path '/uploads/' is missing a trailing slash
Cmove_uploaded_file requires the file size as first argument
DThe file input name should be 'upload' not 'file'
Attempts:
2 left
💡 Hint

Check the first argument of move_uploaded_file. It must be the temporary file path.

📝 Syntax
advanced
1:30remaining
What error does this PHP code produce when accessing $_FILES?

Consider this PHP snippet:

<?php
$file = $_FILES['upload'];
echo $file['name'];
echo $file['tmpname'];
?>

What error will this code produce?

AParse error: syntax error, unexpected '['
BFatal error: Cannot use string offset as an array
CNotice: Undefined index: tmpname
DNo error, outputs the file name and empty string
Attempts:
2 left
💡 Hint

Check the spelling of the keys in the $_FILES array.

🚀 Application
expert
3:00remaining
How many files are uploaded in this PHP script?

Given the following HTML and PHP code, how many files will be processed in the PHP script?

<form method="post" enctype="multipart/form-data">
  <input type="file" name="photos[]" multiple>
  <input type="submit">
</form>

<?php
if (!empty($_FILES['photos'])) {
    echo count($_FILES['photos']['name']);
} else {
    echo 0;
}
?>
A1
BAn error occurs because 'photos' is an array
C0
DThe number of files selected by the user
Attempts:
2 left
💡 Hint

Check how multiple file inputs are handled in $_FILES.