What is the basic Laravel method to handle file uploads in a controller?
Use the store() method on the uploaded file instance, like $request->file('photo')->store('folder') to save the file in storage.
Click to reveal answer
beginner
How do you validate an uploaded file in Laravel?
Use the validate() method on the request with rules like 'file' => 'required|file|mimes:jpg,png|max:2048' to check presence, type, and size.
Click to reveal answer
intermediate
What is the purpose of the public_path() helper in file uploads?
It returns the path to the public folder, useful when you want to move or save uploaded files where they can be accessed by the browser.
Click to reveal answer
intermediate
How can you generate a unique filename for an uploaded file in Laravel?
Use Str::uuid() or time() combined with the original extension, for example: $filename = time() . '.' . $file->getClientOriginalExtension();
Click to reveal answer
intermediate
What is the difference between store() and storeAs() methods in Laravel file uploads?
store() saves the file with a generated unique name, while storeAs() lets you specify the exact filename to save.
Click to reveal answer
Which Laravel method is used to retrieve an uploaded file from a request?
A$request->file('input_name')
B$request->upload('input_name')
C$request->getFile('input_name')
D$request->inputFile('input_name')
✗ Incorrect
The correct method to get an uploaded file is $request->file('input_name').
How do you validate that an uploaded file is an image and less than 2MB in Laravel?
A'file' => 'required|mimes:jpg,png,gif|max:2048'
B'file' => 'required|file|image|max:2048'
C'file' => 'required|image|max:2048'
D'file' => 'required|file|mimes:jpg,png|max:2048'
✗ Incorrect
Use file and image rules together with max:2048 to validate image files under 2MB.
What does the store('folder') method do with an uploaded file?
ASaves the file in the specified folder with a unique name
BReturns the file contents as a string
CDeletes the file from the request
DUploads the file to a remote server
✗ Incorrect
store('folder') saves the file in storage/app/folder with a unique generated filename.
Which helper function returns the path to the public directory in Laravel?
Astorage_path()
Bbase_path()
Cpublic_path()
Dapp_path()
✗ Incorrect
public_path() returns the full path to the public folder where web-accessible files are stored.
If you want to save an uploaded file with a custom name, which method should you use?
Astore()
BsaveAs()
Csave()
DstoreAs()
✗ Incorrect
storeAs() lets you specify the folder and filename to save the uploaded file.
Explain the steps to upload and save a file in Laravel, including validation and storage.
Think about how you handle the file from the form to storage.
You got /5 concepts.
Describe how Laravel helps ensure uploaded files are safe and valid before saving.
Focus on validation and storage safety.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of Laravel's store() method when uploading files?
easy
A. To save the uploaded file to a specified disk or folder
B. To validate the file type and size before upload
C. To display the uploaded file on the webpage
D. To delete the uploaded file from the server
Solution
Step 1: Understand the role of store() in file uploads
The store() method in Laravel is designed to save the uploaded file to a disk or folder configured in the app.
Step 2: Differentiate from validation and display
Validation is done separately using Laravel's validation rules, and displaying files is handled by views or frontend code, not store().
Final Answer:
To save the uploaded file to a specified disk or folder -> Option A
Quick Check:
store() saves files [OK]
Hint: Remember: store() saves files, validation is separate [OK]
Common Mistakes:
Confusing store() with validation methods
Thinking store() displays files
Assuming store() deletes files
2. Which HTML form attribute is required to upload files correctly in Laravel?
easy
A. method="get"
B. enctype="multipart/form-data"
C. action="upload.php"
D. target="_blank"
Solution
Step 1: Identify the form attribute for file uploads
To upload files, the form must have enctype="multipart/form-data" so the browser sends file data properly.
Step 2: Understand other attributes
method="get" is not suitable for file uploads; action specifies URL but is not enough; target controls where to open response.
Final Answer:
enctype="multipart/form-data" -> Option B
Quick Check:
Form needs enctype multipart/form-data [OK]
Hint: File uploads always need enctype multipart/form-data [OK]
Common Mistakes:
Omitting enctype attribute
Using method="get" instead of post
Confusing action URL with enctype
3. Given this Laravel controller code snippet, what will be the value of $path after uploading a file named photo.jpg?
public function upload(Request $request) {
$path = $request->file('image')->store('photos');
return $path;
}
medium
A. "photos/unique_generated_name.jpg"
B. "/storage/photos/photo.jpg"
C. "photos/photo.jpg"
D. "/public/photos/photo.jpg"
Solution
Step 1: Understand how store() names files
The store() method saves the file in the given folder but generates a unique filename to avoid overwriting.
Step 2: Check the returned path format
The returned path is relative to the storage disk root and includes the folder plus the unique filename, not the original name.
Final Answer:
"photos/unique_generated_name.jpg" -> Option A
Quick Check:
store() returns folder + unique filename [OK]
Hint: store() auto-renames files to unique names [OK]
Common Mistakes:
Assuming original filename is kept
Expecting absolute URL path
Confusing storage path with public URL
4. What is wrong with this Laravel file upload code?
public function upload(Request $request) {
$file = $request->file('photo');
$file->storeAs('uploads');
return 'File uploaded';
}
medium
A. The storeAs() method cannot be used in Laravel
B. The file() method should be files()
C. The storeAs() method requires a second argument for filename
D. The uploads folder must be inside storage/app
Solution
Step 1: Check storeAs() method usage
The storeAs() method requires two arguments: destination path and filename. Here only destination is given.
Step 2: Verify other method correctness
file() is correct to get uploaded file; storeAs() is valid but needs filename; folder location depends on app setup.
Final Answer:
The storeAs() method requires a second argument for filename -> Option C
Quick Check:
storeAs() needs destination and filename [OK]
Hint: storeAs() always needs destination path and filename [OK]
Common Mistakes:
Passing only destination to storeAs()
Confusing file() with files()
Assuming storeAs() is invalid in Laravel
5. You want to upload multiple images and save their paths in the database. Which Laravel code snippet correctly handles multiple file uploads and stores their paths in an array?
hard
A. $paths = $request->file('images')->move('gallery');
B. $paths = $request->file('images')->store('gallery');
C. foreach ($request->images as $image) {
$paths[] = $image->move('gallery');
}
D. foreach ($request->file('images') as $image) {
$paths[] = $image->store('gallery');
}
Solution
Step 1: Understand handling multiple files
When uploading multiple files, $request->file('images') returns an array of files, so we loop through each.
Step 2: Use store() inside loop and collect paths
Each file is stored individually with store(), and the returned paths are saved in an array.
Final Answer:
foreach ($request->file('images') as $image) {
$paths[] = $image->store('gallery');
} -> Option D
Quick Check:
Loop files, store each, collect paths [OK]
Hint: Loop multiple files, store each, save paths in array [OK]
Common Mistakes:
Calling store() on array directly
Using move() without filename in loop
Not looping over multiple files
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.