Secure File Uploads in Laravel: Validate, Store and Serve Safely
File uploads are one of the riskiest features in a web application. A careless upload form can let someone store a script on your server, overwrite files or fill your disk. Laravel gives you the tools to do it safely, but you have to use them. This guide covers validation, storage and serving.
What this article is, and is not
This is a security-minded how-to for current Laravel versions. It does not replace a full security review. Virus scanning and image processing are outside its scope.
1. Validate the file
$request->validate([
'avatar' => ['required', 'image', 'mimes:jpg,jpeg,png,webp', 'max:2048'],
]);
image and mimes check the file's detected type, not just its name. Never rely on the extension the user's browser reports.
max is in kilobytes, so 2048 is about 2 MB.
- PHP's own limits (
upload_max_filesize and post_max_size in php.ini) apply first. If they are smaller than your rule, Laravel never sees the file.
2. Let Laravel choose the filename
$path = $request->file('avatar')->store('avatars', 'public');
// e.g. avatars/Qx3f...random.png
store() generates a random name. Do not save a file under the name the user sent: it can contain path tricks, collide with another file, or carry a dangerous extension. If you want to show the original name, save it in a database column instead.
3. Public or private?
The public disk is served directly after php artisan storage:link. Use it only for files that anyone may view, such as profile pictures.
For private files, such as invoices, store them on the default local disk, which is not reachable by URL, and serve them through a controller that checks permission:
public function download(Invoice $invoice)
{
$this->authorize('view', $invoice);
return Storage::disk('local')->download($invoice->path, 'invoice-'.$invoice->number.'.pdf');
}
4. Be careful with risky types
- Do not allow uploads that a browser will run: HTML, SVG with scripts, or any PHP file. If you must accept SVG, sanitise it or serve it so it cannot execute.
- Never store uploads inside a folder where the web server executes PHP.
- For documents, restrict to the types you need, such as
mimes:pdf.
5. Delete old files
Storage::disk('public')->delete($user->avatar_path);
When a user replaces or removes a file, delete the old one. Otherwise the disk fills up quietly.
Test it with a fake disk
Storage::fake('public');
$this->actingAs($user)->post('/profile/avatar', [
'avatar' => UploadedFile::fake()->image('me.png'),
])->assertSessionHasNoErrors();
Storage::disk('public')->assertExists($user->fresh()->avatar_path);
Common mistakes
- Trusting the file extension or the client's reported type.
- Saving with the original filename.
- Putting private documents on the public disk.
- Forgetting that
max is in kilobytes, and that PHP's limits can reject the file first.
Summary
Validate by detected type and size, let Laravel pick the filename, keep private files off the public disk and serve them through an authorized controller, and delete files you no longer need.