Uploading files lets users send pictures, documents, or other data to your website. It helps you save and use these files later.
Uploading files in Laravel
<?php
// In your controller method
public function upload(Request $request)
{
$request->validate([
'file' => 'required|file|max:10240', // max 10MB
]);
$path = $request->file('file')->store('uploads');
return back()->with('success', 'File uploaded to ' . $path);
}Use store() to save files in the default storage folder.
Validate files to check size and type before saving.
<?php // Store file in 'uploads' folder with original name $path = $request->file('file')->storeAs('uploads', $request->file('file')->getClientOriginalName());
<?php // Store file publicly accessible $path = $request->file('file')->store('uploads', 'public');
<?php // Validate file type and size $request->validate([ 'file' => 'required|mimes:jpg,png,pdf|max:5120', ]);
This example shows a simple Laravel controller with two methods: one to display the upload form and one to handle the file upload. The form uses enctype="multipart/form-data" to send files. The controller validates the file and stores it in the 'uploads' folder. After uploading, it shows a success message with the file path.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class FileUploadController extends Controller { public function showForm() { return view('upload-form'); } public function upload(Request $request) { $request->validate([ 'file' => 'required|file|max:2048', // max 2MB ]); $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 use enctype="multipart/form-data" in your HTML form to allow file uploads.
Use Laravel's validation to protect your app from unwanted file types or large files.
Files are stored in the storage/app folder by default. Use php artisan storage:link to make files accessible publicly.
Uploading files lets users send data to your app.
Use Laravel's store() method to save files easily.
Always validate files and set proper form encoding.
Practice
store() method when uploading files?Solution
Step 1: Understand the role of
Thestore()in file uploadsstore()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, notstore().Final Answer:
To save the uploaded file to a specified disk or folder -> Option AQuick Check:
store()saves files [OK]
- Confusing store() with validation methods
- Thinking store() displays files
- Assuming store() deletes files
Solution
Step 1: Identify the form attribute for file uploads
To upload files, the form must haveenctype="multipart/form-data"so the browser sends file data properly.Step 2: Understand other attributes
method="get"is not suitable for file uploads;actionspecifies URL but is not enough;targetcontrols where to open response.Final Answer:
enctype="multipart/form-data" -> Option BQuick Check:
Form needs enctype multipart/form-data [OK]
- Omitting enctype attribute
- Using method="get" instead of post
- Confusing action URL with enctype
$path after uploading a file named photo.jpg?
public function upload(Request $request) {
$path = $request->file('image')->store('photos');
return $path;
}Solution
Step 1: Understand how
Thestore()names filesstore()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 AQuick Check:
store() returns folder + unique filename [OK]
- Assuming original filename is kept
- Expecting absolute URL path
- Confusing storage path with public URL
public function upload(Request $request) {
$file = $request->file('photo');
$file->storeAs('uploads');
return 'File uploaded';
}Solution
Step 1: Check
ThestoreAs()method usagestoreAs()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:
ThestoreAs()method requires a second argument for filename -> Option CQuick Check:
storeAs()needs destination and filename [OK]
- Passing only destination to storeAs()
- Confusing file() with files()
- Assuming storeAs() is invalid in Laravel
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
Each file is stored individually withstore()inside loop and collect pathsstore(), and the returned paths are saved in an array.Final Answer:
foreach ($request->file('images') as $image) { $paths[] = $image->store('gallery'); } -> Option DQuick Check:
Loop files, store each, collect paths [OK]
- Calling store() on array directly
- Using move() without filename in loop
- Not looping over multiple files
Ready to prove it?
Pass the test and this pattern moves unknown
