Hello everyone, in this guide, I'll walk you through the process of implementing multiple file uploads in Laravel 11. whether it's images, documents, or any other type of file. We'll see step by step guide for uploading multiple files at once in laravel 11.
Throughout this tutorial, I'll cover the steps required to set up a Laravel application for handling multiple file uploads.
Laravel 11 Multiple File Upload Example.
Step 1: Install Laravel 11 Application
Step 2: Create Migration and Model
Step 3: Create Controller
Step 4: Create Routes
Step 5: Create Blade File
Step 6: Run the Laravel App
In this step, we'll install the laravel 11 application using the following composer command.
composer create-project laravel/laravel laravel-11-example
After that, we'll create a migration for the files table. So, run the following command to create a migration.
php artisan make:migration create_files_table
Migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('files', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('files');
}
};
Then, run the migration using the following command.
php artisan migrate
After migrating the table we'll create a model using the following command.
php artisan make:model File
app/Models/File.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
use HasFactory;
protected $fillable = [
'name'
];
}
Now, we'll create a FileContrller.php file using the following command. In this file, we'll define the multiple file stores in the database.
php artisan make:controller FileController
app/Http/Controllers/FileController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\File;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class FileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(): View
{
return view('file-upload');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request): RedirectResponse
{
$request->validate([
'files' => 'required|array',
'files.*' => 'required|mimes:pdf,xlx,csv|max:4096',
]);
$files = [];
foreach($request->file('files') as $file) {
// Generate a unique name for the file
$file_name = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension();
// Move the file to the desired location
$file->move(public_path('uploads'), $file_name);
$files[] = ['name' => $file_name];
}
foreach ($files as $fileData) {
File::create($fileData);
}
return back()->with('success', 'Files uploaded successfully!');
}
}
Store File in Storage Folder
$image->storeAs('files', $file_name);
// storage/app/files/test.csv
Store File in Public Folder
$image->move(public_path('files'), $file_name);
// public/images/test.csv
Store File in S3
$image->storeAs('files', $file_name, 's3');
In this step, we'll define routes into the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileController;
Route::get('file-upload', [FileController::class, 'index']);
Route::post('file-upload', [FileController::class, 'store'])->name('file.store');
Then, we'll create a file-upload.blade.php file. In this file, we'll define a file upload form with a submit button.
resources/views/file-upload.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Multiple File Upload in Laravel 11 - Techsolutionstuff</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3"><i class="fa fa-star"></i> How to Multiple File Upload in Laravel 11 - Techsolutionstuff</h3>
<div class="card-body">
@session('success')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
@endsession
<form action="{{ route('file.store') }}" method="POST" enctype="multipart/form-data" class="mt-2">
@csrf
<div class="mb-3">
<label class="form-label" for="input-file">Select Files:</label>
<input
type="file"
name="files[]"
id="input-file"
multiple
class="form-control @error('files') is-invalid @enderror">
@error('files')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Upload</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
In this step, we'll run the laravel 11 application using the following command.
php artisan serve
You might also like: