Bird
Raised Fist0
PHPprogramming~20 mins

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

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

Practice

(1/5)
1. What does the $_FILES superglobal in PHP store?
easy
A. Database connection details
B. User session data
C. Information about files uploaded via an HTML form
D. Server environment variables

Solution

  1. Step 1: Understand the purpose of $_FILES

    $_FILES is a special PHP variable that holds details about files uploaded through a form.
  2. Step 2: Compare with other options

    User session data is stored in $_SESSION, database details are not stored here, and server variables are in $_SERVER.
  3. Final Answer:

    Information about files uploaded via an HTML form -> Option C
  4. Quick Check:

    $_FILES = Uploaded file info [OK]
Hint: Remember: $_FILES always relates to uploaded files [OK]
Common Mistakes:
  • Confusing $_FILES with $_SESSION
  • Thinking $_FILES holds server info
  • Assuming $_FILES stores form text inputs
2. Which of the following is the correct way to access the name of an uploaded file with input name userfile?
easy
A. $_FILES['userfile']['filename']
B. $_FILES['userfile']['file']
C. $_FILES['userfile']['file_name']
D. $_FILES['userfile']['name']

Solution

  1. Step 1: Recall the structure of $_FILES

    Each uploaded file's info is stored in an array with keys like 'name', 'type', 'tmp_name', 'error', and 'size'.
  2. Step 2: Identify the correct key for the original filename

    The key for the original filename is 'name', not 'filename' or 'file_name'.
  3. Final Answer:

    $_FILES['userfile']['name'] -> Option D
  4. Quick Check:

    Filename key = 'name' [OK]
Hint: Use 'name' key to get original filename from $_FILES [OK]
Common Mistakes:
  • Using 'filename' or 'file_name' instead of 'name'
  • Confusing keys with other $_FILES elements
  • Trying to access $_POST for file names
3. What will the following PHP code output if a file named 'photo.jpg' is uploaded successfully?
if ($_FILES['pic']['error'] === 0) {
    echo $_FILES['pic']['name'];
} else {
    echo 'Upload failed';
}
medium
A. photo.jpg
B. Upload failed
C. 0
D. Undefined index error

Solution

  1. Step 1: Check the error code for the uploaded file

    An error code of 0 means the upload was successful with no errors.
  2. Step 2: Output the original filename

    Since error is 0, the code echoes the original filename stored in $_FILES['pic']['name'], which is 'photo.jpg'.
  3. Final Answer:

    photo.jpg -> Option A
  4. Quick Check:

    Error 0 means success, so filename prints [OK]
Hint: Error 0 means success; print 'name' to get filename [OK]
Common Mistakes:
  • Assuming error 0 means failure
  • Printing error code instead of filename
  • Not checking if 'pic' index exists
4. Identify the error in this PHP snippet handling file upload:
if ($_FILES['upload']['error'] == 0) {
    move_uploaded_file($_FILES['upload']['name'], 'uploads/' . $_FILES['upload']['name']);
    echo 'File saved';
} else {
    echo 'Error uploading';
}
medium
A. Not checking file size before moving
B. Using 'name' instead of 'tmp_name' in move_uploaded_file
C. Checking error with == instead of ===
D. Missing semicolon after echo statement

Solution

  1. Step 1: Understand move_uploaded_file parameters

    The first parameter must be the temporary file path stored in tmp_name, not the original filename.
  2. Step 2: Identify the incorrect usage

    The code incorrectly uses $_FILES['upload']['name'] as the source file, which is wrong and causes failure.
  3. Final Answer:

    Using 'name' instead of 'tmp_name' in move_uploaded_file -> Option B
  4. Quick Check:

    move_uploaded_file needs tmp_name as source [OK]
Hint: Use 'tmp_name' as source file in move_uploaded_file [OK]
Common Mistakes:
  • Using original filename instead of temp file path
  • Ignoring error codes before moving files
  • Not validating file size or type
5. You want to save an uploaded file only if it is less than 2MB and has no errors. Which code snippet correctly checks these conditions before moving the file?
if (/* condition here */) {
    move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
    echo 'Upload successful';
} else {
    echo 'Upload failed';
}
hard
A. if ($_FILES['file']['error'] === 0 && $_FILES['file']['size'] < 2097152)
B. if ($_FILES['file']['error'] == 1 || $_FILES['file']['size'] > 2097152)
C. if ($_FILES['file']['error'] !== 0 && $_FILES['file']['size'] <= 2097152)
D. if ($_FILES['file']['error'] === 0 || $_FILES['file']['size'] > 2097152)

Solution

  1. Step 1: Check for no upload errors

    The error code must be exactly 0 to confirm no errors during upload.
  2. Step 2: Check file size limit

    The file size must be less than 2MB (2 * 1024 * 1024 = 2097152 bytes).
  3. Step 3: Combine conditions correctly

    Both conditions must be true, so use logical AND (&&) with strict equality and less-than size check.
  4. Final Answer:

    if ($_FILES['file']['error'] === 0 && $_FILES['file']['size'] < 2097152) -> Option A
  5. Quick Check:

    Error 0 and size < 2MB = correct check [OK]
Hint: Use error === 0 and size < 2MB with && [OK]
Common Mistakes:
  • Using || instead of && for conditions
  • Checking error != 0 instead of === 0
  • Confusing size units or limits
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