0
0
Laravelframework~20 mins

File uploads 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 in Laravel?

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
<?php
public function upload(Request $request) {
    $path = $request->file('document')->store('uploads');
    return back()->with('message', 'File uploaded!');
}
AThe upload fails silently and no file is stored.
BThe file is stored regardless of type or size, which may cause security or storage issues.
CLaravel automatically rejects files larger than 2MB without any code.
DLaravel throws a validation error automatically for unsupported file types.
Attempts:
2 left
💡 Hint

Think about what happens if you don't tell Laravel to check the file before saving.

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

Choose the code snippet that stores an uploaded file with a custom name preserving the original extension.

A
$file = $request-&gt;file('photo');
$filename = 'user_'.time().'.'.$file-&gt;extension();
$path = $file-&gt;storeAs('avatars', $filename);
B
$file = $request-&gt;file('photo');
$filename = 'user_'.time();
$path = $file-&gt;storeAs('avatars', $filename);
C
$file = $request-&gt;file('photo');
$filename = 'user_'.time().'.'.$file-&gt;getClientOriginalExtension();
$path = $file-&gt;storeAs('avatars', $filename);
D
$file = $request-&gt;file('photo');
$filename = 'user_'.time().'.jpg';
$path = $file-&gt;store('avatars', $filename);
Attempts:
2 left
💡 Hint

Remember to preserve the original file extension and use the correct method to store with a custom name.

🔧 Debug
advanced
2:00remaining
Why does this Laravel file upload code throw an error?

Examine the code below. It throws an error when uploading a file. What is the cause?

Laravel
<?php
public function upload(Request $request) {
    $file = $request->file('document');
    $path = $file->store('uploads', 'public');
    return back()->with('message', 'Uploaded!');
}
AThe code is missing a validation step, so Laravel blocks the upload.
BThe 'public' disk is not configured in config/filesystems.php, causing a runtime error.
CThe store method requires a filename, so this code throws a syntax error.
DThe file variable is null because the input name is incorrect, causing a call on null error.
Attempts:
2 left
💡 Hint

Check if the file input name matches the request key.

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

Given the code below, what will be the value of $filename if the uploaded file is named report.pdf?

Laravel
<?php
$file = $request->file('upload');
$filename = $file->hashName();
AA unique hashed string with the original extension, e.g., '3f1e2d4a5b6c7d8e9f0a.pdf'.
BThe original filename 'report.pdf' without changes.
CA timestamp string like '1680000000.pdf'.
DAn error because hashName requires a parameter.
Attempts:
2 left
💡 Hint

hashName generates a unique name with the original extension.

🧠 Conceptual
expert
3:00remaining
Which Laravel feature ensures uploaded files are safely stored and accessible only as intended?

Among the options below, which Laravel feature best helps protect uploaded files from unauthorized access while allowing controlled retrieval?

AUsing Laravel's Storage facade with private disks and generating temporary signed URLs for access.
BSaving files in the database as blobs to prevent direct file access.
CStoring files in the public directory and linking them directly via URL.
DEncrypting files manually before upload and decrypting on download.
Attempts:
2 left
💡 Hint

Think about Laravel's built-in tools for file storage and secure access.