File uploads let users send files like images or documents to your website. Laravel makes handling these uploads easy and safe.
File uploads in Laravel
<?php
// In your controller method handling the form submission
public function uploadFile(Request $request) {
$request->validate([
'file' => 'required|file|mimes:jpg,png,pdf|max:2048',
]);
$path = $request->file('file')->store('uploads');
return back()->with('success', 'File uploaded to ' . $path);
}
Use $request->file('input_name') to get the uploaded file.
The store method saves the file in the storage/app/uploads folder by default.
<?php // Validate and store an image file $request->validate(['image' => 'required|image|max:1024']); $path = $request->file('image')->store('images');
<?php // Store file with original name $file = $request->file('document'); $filename = $file->getClientOriginalName(); $file->storeAs('docs', $filename);
<?php // Check if file was uploaded if ($request->hasFile('photo')) { $path = $request->file('photo')->store('photos'); }
This example shows a simple Laravel controller with two methods: one to show the upload form and one to handle the file upload. The form uses enctype="multipart/form-data" to allow file sending. The controller validates the file type and size, stores it, and returns a success message.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class FileUploadController extends Controller { public function showForm() { return view('upload-form'); } public function uploadFile(Request $request) { $request->validate([ 'file' => 'required|file|mimes:jpg,png,pdf|max:2048', ]); $path = $request->file('file')->store('uploads'); return back()->with('success', 'File uploaded to ' . $path); } } // Blade view: resources/views/upload-form.blade.php // // <form method="POST" action="{{ route('file.upload') }}" enctype="multipart/form-data"> // @csrf // <input type="file" name="file" aria-label="Choose file to upload" required> // <button type="submit">Upload</button> // </form> // // @if(session('success')) // <p>{{ session('success') }}</p> // @endif
Always validate uploaded files to avoid security risks.
Use enctype="multipart/form-data" in your HTML form to enable file uploads.
Files are stored in storage/app by default. Use php artisan storage:link to make them accessible publicly.
Laravel makes file uploads simple with built-in methods.
Always validate files for type and size before saving.
Use proper form encoding and check if files exist before processing.
Practice
Solution
Step 1: Identify Laravel method to check file upload
Laravel provideshasFileto verify if a file was uploaded in the request.Step 2: Compare options with Laravel documentation
OnlyhasFileis the correct method; others do not exist in Laravel.Final Answer:
$request->hasFile('file') -> Option BQuick Check:
Check file upload with hasFile() = D [OK]
- Using non-existent methods like fileExists()
- Confusing hasFile() with file() method
- Not checking if file exists before processing
Solution
Step 1: Identify form attribute for file uploads
File uploads require the form to haveenctype="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;actiondefines URL, not upload encoding.Final Answer:
enctype="multipart/form-data" -> Option AQuick Check:
Form encoding for files = B [OK]
- Omitting enctype attribute
- Using GET method instead of POST
- Confusing input type with form attribute
photo.jpg?
$path = $request->file('photo')->store('images');
return $path;Solution
Step 1: Understand store() method behavior
Thestore()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 DQuick Check:
store() returns folder + unique filename = A [OK]
- Assuming original filename is kept
- Expecting full storage path returned
- Confusing store() with move() method
if ($request->hasFile('avatar')) {
$file = $request->file('avatar');
$file->move('uploads');
}Solution
Step 1: Review move() method signature
Themove()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 AQuick Check:
move() needs path and filename = C [OK]
- Calling move() with only one argument
- Confusing hasFile() with fileExists()
- Calling move() on request instead of file
photos?Solution
Step 1: Understand multiple file upload handling
Multiple files come as an array; you must loop through each file to store and save paths.Step 2: Analyze options for correct looping and storing
foreach ($request->file('photos') as $photo) { $path = $photo->store('gallery'); Photo::create(['path' => $path]); }loops over each file, stores it, and saves path correctly. Others treat files as single or misuse methods.Final Answer:
Loop through each file, store, and save path -> Option CQuick Check:
Multiple files need foreach loop = A [OK]
- Treating multiple files as single file
- Not looping through file array
- Calling store() on file array directly
Ready to prove it?
Pass the test and this pattern moves unknown
