Laravel File Upload & Storage (Local + S3)
Laravel file upload is a common requirement in modern web applications, whether it’s user avatars, documents, or media files.
Moreover, Laravel provides a powerful and simple API to handle file uploads and storage using both local and cloud drivers like Amazon S3.
In this guide, we will learn how to upload files in Laravel and store them using Local Storage and Amazon S3.
Understanding Laravel Filesystem
Laravel uses the Filesystem abstraction powered by Flysystem. It supports multiple storage drivers:
- Local (default)
- Public
- Amazon S3
- FTP / SFTP
All configurations are defined in the config/filesystems.php file.
Basic File Upload in Laravel
Step 1: Create a Form
|
1 2 3 4 5 |
<form action="/upload" method="POST" enctype="multipart/form-data"> @csrf <input type="file" name="file"> <button type="submit">Upload</button> </form> |
Step 2: Handle Upload in Controller
|
1 2 3 4 5 6 7 8 9 10 11 12 |
use Illuminate\Http\Request; public function upload(Request $request) { $request->validate([ 'file' => 'required|file|mimes:jpg,png,pdf|max:2048' ]); $path = $request->file('file')->store('uploads'); return "File stored at: " . $path; } |
Storing Files in Local Storage
By default, Laravel stores files in the storage/app directory.
Example
|
1 |
$path = $request->file('file')->store('uploads'); |
This will store the file in:
|
1 |
storage/app/uploads |
Public Access
To make files publicly accessible:
|
1 |
php artisan storage:link |
Then store files like this:
|
1 |
$path = $request->file('file')->store('uploads', 'public'); |
Now files will be accessible via:
|
1 |
/storage/uploads/filename.jpg |
Custom File Names
|
1 2 3 4 5 |
$file = $request->file('file'); $filename = time() . '_' . $file->getClientOriginalName(); $path = $file->storeAs('uploads', $filename); |
File Upload Using Storage Facade
|
1 2 3 |
use Illuminate\Support\Facades\Storage; Storage::put('uploads/file.txt', file_get_contents($request->file('file'))); |
Uploading Files to Amazon S3
1: Install AWS SDK
|
1 |
composer require league/flysystem-aws-s3-v3 |
2: Configure .env
|
1 2 3 4 5 |
AWS_ACCESS_KEY_ID=your_key AWS_SECRET_ACCESS_KEY=your_secret AWS_DEFAULT_REGION=ap-south-1 AWS_BUCKET=your_bucket_name AWS_URL=https://your-bucket.s3.amazonaws.com |
3: Configure Filesystem
|
1 2 3 4 5 6 7 |
's3' => [ 'driver' => 's3', 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION'), 'bucket' => env('AWS_BUCKET'), ], |
4: Upload File to S3
|
1 |
$path = $request->file('file')->store('uploads', 's3'); |
Making Files Public on S3
|
1 2 3 |
$path = $request->file('file')->store('uploads', 's3'); Storage::disk('s3')->setVisibility($path, 'public'); |
Get File URL
|
1 |
$url = Storage::disk('s3')->url($path); |
Deleting Files
|
1 |
|
1 2 3 4 |
Storage::delete('uploads/file.jpg'); // S3 Storage::disk('s3')->delete('uploads/file.jpg');<code dir="ltr"> |
|
1 |
Checking File Exists
|
1 2 3 |
if (Storage::exists('uploads/file.jpg')) { // File exists } |
Validation for File Uploads
|
1 2 3 |
$request->validate([ 'file' => 'required|file|mimes:jpg,png,pdf|max:2048' ]); |
- mimes: allowed file types
- max: size in KB
Best Practices
- Always validate file uploads
- Use unique file names
- Store sensitive files privately
- Use S3 for scalable applications
- Clean unused files regularly
Local vs S3 Comparison
| Feature | Local Storage | Amazon S3 |
|---|---|---|
| Setup | Easy | Moderate |
| Cost | Free | Paid |
| Scalability | Limited | High |
| Performance | Fast (local) | CDN optimized |
| Best For | Small apps | Production apps |
Conclusion
Laravel makes file uploads and storage extremely simple with its powerful filesystem abstraction.
Use local storage for development or small apps, and switch to Amazon S3 for scalability and production environments.
With just a few lines of code, you can handle uploads, manage files, and scale your storage effortlessly.
Frequently Asked Questions
Where are files stored in Laravel?
By default, files are stored in the storage/app directory.
How do I access uploaded files?
Use php artisan storage:link and access via /storage URL.
Is S3 better than local storage?
Yes, for production apps due to scalability and reliability.
Can I use multiple storage disks?
Yes, Laravel supports multiple disks like local, public, and S3.
Final Tip
Start with local storage during development, and switch to S3 when your application grows.
You can also hire laravel developers to build your custom solutions on laravel.
For exploring the available extensions for Bagisto, you can check out the bagisto extension marketplace.