0
0
Laravelframework~20 mins

Uploading files in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel File Upload Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a file is uploaded without validation?

Consider a Laravel controller method that handles file uploads but does not validate the file type or size. What is the likely outcome when a user uploads a very large or unsupported file?

Laravel
public function upload(Request $request) {
    $path = $request->file('photo')->store('photos');
    return 'File uploaded to ' . $path;
}
AThe file is saved regardless of type or size, which may cause storage or security issues.
BLaravel automatically rejects files larger than 2MB without any code.
CThe upload fails silently and no file is saved.
DLaravel throws a validation error automatically for unsupported file types.
Attempts:
2 left
💡 Hint

Think about what Laravel does by default if you don't add validation rules.

📝 Syntax
intermediate
2:00remaining
Which code correctly stores an uploaded file with a custom filename?

Given a file input named 'document', which Laravel code snippet correctly stores the file with a custom name 'user_123.pdf' in the 'docs' folder?

A$request->file('document')->storeAs('docs', 'user_123.pdf');
B$request->file('document')->store('docs/user_123.pdf');
C$request->file('document')->move('docs', 'user_123.pdf');
D$request->file('document')->saveAs('docs', 'user_123.pdf');
Attempts:
2 left
💡 Hint

Look for the Laravel method that allows specifying a custom filename.

🔧 Debug
advanced
2:00remaining
Why does this file upload code fail with 'Call to a member function store() on null'?

Examine the following Laravel controller method:

public function upload(Request $request) {
    $path = $request->file('photo')->store('images');
    return $path;
}

When submitting the form, the error occurs. What is the cause?

AThe file is too large and Laravel rejects it silently.
BThe 'photo' input name does not match the file input name in the form.
CThe storage folder 'images' does not exist and causes the error.
DThe form does not have <code>enctype="multipart/form-data"</code>, so no file is sent.
Attempts:
2 left
💡 Hint

Check the HTML form attributes required for file uploads.

state_output
advanced
2:00remaining
What is the value of $filename after this code runs?

Consider this Laravel code snippet:

$file = $request->file('avatar');
$filename = time() . '.' . $file->getClientOriginalExtension();
$file->storeAs('avatars', $filename);

Assuming the uploaded file is named 'photo.png' and the current UNIX timestamp is 1680000000, what is the value of $filename?

Aavatars/1680000000.png
Bphoto.png
C1680000000.png
D1680000000.photo.png
Attempts:
2 left
💡 Hint

Look at how the filename is constructed using time() and the file extension.

🧠 Conceptual
expert
3:00remaining
Which Laravel feature helps prevent overwriting files with the same name during upload?

When multiple users upload files with the same original filename, what Laravel feature or approach ensures each file is saved uniquely without overwriting?

AUsing <code>storeAs()</code> with the original filename to keep names consistent.
BUsing <code>store()</code> method which generates a unique ID for the filename automatically.
CManually renaming files with <code>getClientOriginalName()</code> before storing.
DSaving files in the public folder without any naming changes.
Attempts:
2 left
💡 Hint

Think about which method Laravel uses to avoid filename collisions automatically.