This content originally appeared on DEV Community and was authored by Rohit Dhiman
File upload is one of the most common features in modern web applications — whether it’s profile pictures, resumes, PDFs, or images. Luckily, Laravel makes file handling incredibly simple and secure with built-in methods.
In this article, we’ll walk through a step-by-step guide to uploading files in Laravel, complete with validation, storage, and best practices.
Why File Uploads Matter
Here are some real-world scenarios where file uploads are essential:
Profile Pictures – Social media & forums
Documents – Resumes, reports, PDFs
Media Uploads – Images, videos, audio files
Admin Tools – Uploading configuration files
File uploads are everywhere — and mastering them is a must for every Laravel beginner.
Step 1: Create a Route
We need two routes: one for displaying the upload form, and another for handling the upload.
use App\Http\Controllers\FileUploadController;
Route::get('/upload', [FileUploadController::class, 'index']);
Route::post('/upload', [FileUploadController::class, 'store'])->name('file.upload');
Step 2: Build the Controller
Laravel makes file handling painless with $request->file()
.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FileUploadController extends Controller
{
public function index()
{
return view('upload');
}
public function store(Request $request)
{
// Validate file
$request->validate([
'file' => 'required|mimes:jpg,png,pdf|max:2048',
]);
// Store file inside storage/app/public/uploads
$path = $request->file('file')->store('uploads', 'public');
// Return success message
return back()
->with('success', 'File uploaded successfully!')
->with('file', $path);
}
}
Step 3: Create the Blade View
Now let’s create a simple form for file uploads.
<!DOCTYPE html>
<html>
<head>
<title>Laravel File Upload</title>
</head>
<body>
<h2>Upload a File</h2>
@if(session('success'))
<p style="color: green;">{{ session('success') }}</p>
<p>File Path: {{ session('file') }}</p>
@endif
<form action="{{ route('file.upload') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="file">
<button type="submit">Upload</button>
</form>
@error('file')
<p style="color: red;">{{ $message }}</p>
@enderror
</body>
</html>
Notice the important attribute:
enctype="multipart/form-data"
This is required for file uploads.
Step 4: Storage Setup
By default, Laravel stores files in storage/app
. If you want to access them via the browser, run:
php artisan storage:link
This creates a public/storage
link, making your uploaded files accessible.
Best Practices for File Uploads
Validate file types – prevent harmful uploads
Limit file size – avoid server overload
Use storage disks (
public
, s3
, etc.)
Keep sensitive files private
Handle unique file names – Laravel’s
store()
does this automatically
Key Takeaway
Uploading files in Laravel is beginner-friendly yet powerful. Once you’ve mastered the basics, you can extend this to:
Multiple file uploads
Cloud storage (AWS S3, DigitalOcean Spaces)
Image resizing & optimization
Next Steps
Try extending this tutorial to multiple file uploads
Experiment with cloud storage instead of local
Secure sensitive files by storing them outside
public
Wrapping Up
File uploads are a core skill in Laravel development. With just a few lines of code, you can handle files securely and efficiently.
This content originally appeared on DEV Community and was authored by Rohit Dhiman