Bird
0
0

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?

hard📝 Application Q15 of 15
PHP - Superglobals and Web Context
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';
}
Aif ($_FILES['file']['error'] === 0 && $_FILES['file']['size'] < 2097152)
Bif ($_FILES['file']['error'] == 1 || $_FILES['file']['size'] > 2097152)
Cif ($_FILES['file']['error'] !== 0 && $_FILES['file']['size'] <= 2097152)
Dif ($_FILES['file']['error'] === 0 || $_FILES['file']['size'] > 2097152)
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes