Kritim Yantra
Mar 26, 2025
Uploading multiple files in Laravel is a common requirement for applications that handle images, documents, or media. Laravel makes it easy to process multiple file uploads with built-in validation and storage features.
In this guide, we’ll cover how to upload multiple files in Laravel 12 with detailed steps, including form setup, validation, and storage.
Before starting, ensure you have:
First, create a Blade view (resources/views/upload.blade.php
) with a form that supports multiple file uploads:
<!DOCTYPE html>
<html>
<head>
<title>Upload Multiple Files</title>
</head>
<body>
<h1>Upload Multiple Files</h1>
<form action="{{ route('upload.files') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="files[]" multiple>
<button type="submit">Upload</button>
</form>
@if(session('success'))
<div style="color: green;">
{{ session('success') }}
</div>
@endif
@if($errors->any())
<div style="color: red;">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</body>
</html>
enctype="multipart/form-data"
is required for file uploads.name="files[]"
allows multiple file selection.multiple
enables multi-select in the file input.Define a route in routes/web.php
:
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileUploadController;
Route::get('/upload', function () {
return view('upload');
});
Route::post('/upload', [FileUploadController::class, 'upload'])->name('upload.files');
Generate a controller:
php artisan make:controller FileUploadController
Then, update app/Http/Controllers/FileUploadController.php
:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class FileUploadController extends Controller
{
public function upload(Request $request)
{
// Validate files
$request->validate([
'files.*' => 'required|file|mimes:jpg,png,pdf|max:2048', // 2MB max per file
]);
// Check if files were uploaded
if ($request->hasFile('files')) {
foreach ($request->file('files') as $file) {
// Store each file in the 'public/uploads' directory
$path = $file->store('uploads', 'public');
// Optional: Save file info to the database
// File::create(['filename' => $path]);
}
return back()->with('success', 'Files uploaded successfully!');
}
return back()->with('error', 'No files selected.');
}
}
files.*
validates each file in the array.mimes:jpg,png,pdf
restricts file types.max:2048
sets a 2MB limit per file.store('uploads', 'public')
saves files in storage/app/public/uploads
.By default, Laravel stores files in storage/app
. To make them publicly accessible:
Create a symbolic link (to link storage/app/public
to public/storage
):
php artisan storage:link
Configure config/filesystems.php
(optional):
'disks' => [
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
],
To display uploaded files, modify the Blade view (upload.blade.php
):
@if(Storage::disk('public')->exists('uploads'))
<h2>Uploaded Files:</h2>
<ul>
@foreach(Storage::disk('public')->files('uploads') as $file)
<li>
<a href="{{ Storage::url($file) }}" target="_blank">
{{ basename($file) }}
</a>
</li>
@endforeach
</ul>
@endif
Storage::url()
generates a public URL (e.g., http://your-app/storage/uploads/file.jpg
).basename()
extracts the filename from the path.To track uploaded files, create a files
table:
Generate a migration & model:
php artisan make:model File -m
Update the migration (database/migrations/..._create_files_table.php
):
public function up()
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('filename');
$table->timestamps();
});
}
Run the migration:
php artisan migrate
Update the FileUploadController
to save records:
use App\Models\File;
foreach ($request->file('files') as $file) {
$path = $file->store('uploads', 'public');
File::create(['filename' => $path]);
}
upload_max_filesize
and post_max_size
in php.ini
.mimes:
in validation.$file->hashName()
instead of original names:$path = $file->storeAs('uploads', $file->hashName(), 'public');
You’ve now learned how to:
✅ Upload multiple files in Laravel 12
✅ Validate file types and sizes
✅ Store files securely
✅ Display uploaded files
✅ Save file data in the database
This approach works for images, PDFs, and other file types, making it versatile for various applications.
Now you can implement multiple file uploads in your Laravel app with confidence! 🚀
No comments yet. Be the first to comment!
Please log in to post a comment:
Continue with Google