📖 Scenario: You are building a simple Laravel web app where users can upload profile pictures. The app needs to accept image files and store them safely on the server.
🎯 Goal: Build a Laravel file upload feature that accepts an image file from a form, validates it, and saves it to the storage/app/public directory.
📋 What You'll Learn
Create a form with a file input named profile_picture
Add a validation rule to accept only image files
Store the uploaded file in the public disk
Return the stored file path after upload
💡 Why This Matters
🌍 Real World
File uploads are common in web apps for profile pictures, documents, and media. Handling uploads securely and correctly is essential.
💼 Career
Backend developers often implement file upload features. Knowing Laravel's file handling and validation is important for building real-world applications.
Progress0 / 4 steps
1
Create the file upload form
Create a Blade view file with a form that uses POST method and enctype="multipart/form-data". Add a file input with the name profile_picture and a submit button.
Laravel
Hint
Remember to set enctype="multipart/form-data" on the form to allow file uploads.
2
Add route and controller method
In routes/web.php, add a POST route for /upload that uses UploadController@store. Then create UploadController with a store method stub.
Laravel
Hint
Use Route::post with the controller class and method array syntax.
3
Validate and store the uploaded file
In the store method, validate the profile_picture input to accept only image files. Then store the file on the public disk using store('profile_pictures', 'public'). Save the returned path in a variable called path.
Laravel
Hint
Use Laravel's validate method and the image rule to accept only images.
4
Return the stored file path
Complete the store method by returning the stored file path $path as a plain string response using return $path;.
Laravel
Hint
Use return $path; to send back the stored file path as a response.
Practice
(1/5)
1. In Laravel, which method is used to check if a file was uploaded in a request?
easy
A. $request->isFile('file')
B. $request->hasFile('file')
C. $request->checkFile('file')
D. $request->fileExists('file')
Solution
Step 1: Identify Laravel method to check file upload
Laravel provides hasFile to verify if a file was uploaded in the request.
Step 2: Compare options with Laravel documentation
Only hasFile is the correct method; others do not exist in Laravel.
Final Answer:
$request->hasFile('file') -> Option B
Quick Check:
Check file upload with hasFile() = D [OK]
Hint: Use hasFile() to check uploaded files quickly [OK]
Common Mistakes:
Using non-existent methods like fileExists()
Confusing hasFile() with file() method
Not checking if file exists before processing
2. Which HTML form attribute is required to upload files in Laravel?
easy
A. enctype="multipart/form-data"
B. method="post"
C. action="upload.php"
D. type="file"
Solution
Step 1: Identify form attribute for file uploads
File uploads require the form to have enctype="multipart/form-data" to send file data properly.
Step 2: Check other attributes
method="post" is needed but not enough alone; type="file" is for input, not form; action defines URL, not upload encoding.
Final Answer:
enctype="multipart/form-data" -> Option A
Quick Check:
Form encoding for files = B [OK]
Hint: Always set enctype to multipart/form-data for file uploads [OK]
Common Mistakes:
Omitting enctype attribute
Using GET method instead of POST
Confusing input type with form attribute
3. What will be the output of this Laravel code snippet after uploading a file named photo.jpg?
The store() method saves the file in the specified folder with a unique generated filename to avoid overwriting.
Step 2: Analyze returned path
The returned path includes the folder and the unique filename, not the original filename or full storage path.
Final Answer:
"images/unique_generated_name.jpg" -> Option D
Quick Check:
store() returns folder + unique filename = A [OK]
Hint: store() saves file with unique name, returns folder plus new filename [OK]
Common Mistakes:
Assuming original filename is kept
Expecting full storage path returned
Confusing store() with move() method
4. Identify the error in this Laravel file upload code:
if ($request->hasFile('avatar')) {
$file = $request->file('avatar');
$file->move('uploads');
}
medium
A. Missing second argument for move() method
B. Incorrect method to check file existence
C. move() should be called on $request, not $file
D. No error, code is correct
Solution
Step 1: Review move() method signature
The move() method requires two arguments: destination path and filename.
Step 2: Check code arguments
Only one argument ('uploads') is given; filename is missing, causing an error.
Final Answer:
Missing second argument for move() method -> Option A
Quick Check:
move() needs path and filename = C [OK]
Hint: move() needs destination path and filename both [OK]
Common Mistakes:
Calling move() with only one argument
Confusing hasFile() with fileExists()
Calling move() on request instead of file
5. You want to upload multiple images and save their paths in the database. Which Laravel approach correctly handles multiple file uploads in a request named photos?
hard
A.
if ($request->hasFile('photos')) {
$path = $request->file('photos')->store('gallery');
Photo::create(['path' => $path]);
}