Bird
0
0

What will be the result of this Laravel code if no file is uploaded?

medium📝 Predict Output Q4 of 15
Laravel - Request and Response
What will be the result of this Laravel code if no file is uploaded?
$file = $request->file('document');
if ($file) {
echo 'File uploaded';
} else {
echo 'No file';
}
AError: Undefined variable
BFile uploaded
CNo file
DEmpty string
Step-by-Step Solution
Solution:
  1. Step 1: Understand $request->file() behavior when no file

    If no file is uploaded, $request->file('document') returns null.
  2. Step 2: Evaluate the if condition

    The if condition checks if $file is truthy; null is falsy, so else block runs.
  3. Final Answer:

    No file -> Option C
  4. Quick Check:

    Null file check = No file [OK]
Quick Trick: Check if file exists before processing to avoid errors [OK]
Common Mistakes:
  • Assuming file() always returns an object
  • Not checking for null before using file
  • Expecting error instead of else block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes