Laravel's Storage system makes file uploads short and safe.
1. The form
The form must use method="POST" and enctype="multipart/form-data", otherwise the file is not sent.
2. Validate
'photo' => 'required|image|mimes:jpg,jpeg,png,webp|max:2048' — max is in kilobytes (2048 = 2 MB).
3. Store
$path = $request->file('photo')->store('photos', 'public');
Laravel gives the file a random unique name and returns a path like photos/aB3x9.jpg. Save that path in the database.
4. Make it public
Run php artisan storage:link once. It links public/storage to storage/app/public, so files on the public disk get a URL.
5. Show and delete
- Show:
<img src="{{ asset('storage/' . $user->photo) }}"> - Delete:
Storage::disk('public')->delete($user->photo)
Private files
Files such as invoices or ID documents should go on the default local disk (not public) and be sent through a controller that checks permission, using Storage::download($path).
Example
<?php
use Illuminate\Support\Facades\Storage;
public function updatePhoto(Request $request)
{
$request->validate([
'photo' => 'required|image|mimes:jpg,jpeg,png,webp|max:2048',
]);
$user = $request->user();
if ($user->photo) {
Storage::disk('public')->delete($user->photo); // remove the old photo
}
$user->update([
'photo' => $request->file('photo')->store('photos', 'public'),
]);
return back()->with('success', 'Photo updated');
}
/* Blade form:
<form method="POST" action="{{ route('profile.photo') }}" enctype="multipart/form-data">
@csrf
<input type="file" name="photo" accept="image/*">
<button>Upload</button>
</form> */
The file is saved as storage/app/public/photos/<random-name>.jpg and shown with asset('storage/' . $user->photo). The old photo is deleted first.