Updated 30 June 2026
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.
Laravel uses the Filesystem abstraction powered by Flysystem. It supports multiple storage drivers:
All configurations are defined in the config/filesystems.php file.
|
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> |
|
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; } |
By default, Laravel stores files in the storage/app directory.
|
1 |
$path = $request->file('file')->store('uploads'); |
This will store the file in:
|
1 |
storage/app/uploads |
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 |
|
1 2 3 4 5 |
$file = $request->file('file'); $filename = time() . '_' . $file->getClientOriginalName(); $path = $file->storeAs('uploads', $filename); |
|
1 2 3 |
use Illuminate\Support\Facades\Storage; Storage::put('uploads/file.txt', file_get_contents($request->file('file'))); |
|
1 |
composer require league/flysystem-aws-s3-v3 |
|
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 |
|
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'), ], |
|
1 |
$path = $request->file('file')->store('uploads', 's3'); |
|
1 2 3 |
$path = $request->file('file')->store('uploads', 's3'); Storage::disk('s3')->setVisibility($path, 'public'); |
|
1 |
$url = Storage::disk('s3')->url($path); |
|
1 |
|
1 2 3 4 |
Storage::delete('uploads/file.jpg'); // S3 Storage::disk('s3')->delete('uploads/file.jpg');<code dir="ltr"> |
|
1 |
|
1 2 3 |
if (Storage::exists('uploads/file.jpg')) { // File exists } |
|
1 2 3 |
$request->validate([ 'file' => 'required|file|mimes:jpg,png,pdf|max:2048' ]); |
| 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 |
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.
By default, files are stored in the storage/app directory.
Use php artisan storage:link and access via /storage URL.
Yes, for production apps due to scalability and reliability.
Yes, Laravel supports multiple disks like local, public, and S3.
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.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.