Laravel 12 Multiple File Upload: Step by Step Guide

Uploading multiple files in Laravel 12 is a common requirement for many applications. Whether you're handling images, documents, or any other file type, Laravel makes it easy with its powerful file storage system.

In this tutorial, I will guide you through the entire process of implementing multiple file uploads in Laravel 12 with validation and storage.

Laravel 12 Multiple  File Upload: Step by Step Guide

Laravel 12 Multiple  File Upload: Step by Step Guide

 

Step 1: Install Laravel 12

First, if you haven’t installed Laravel 12 yet, you can create a new Laravel project using Composer:

composer create-project laravel/laravel laravel12-multi-upload

Navigate to the project directory:

cd laravel12-multi-upload

 

Step 2: Configure Database

Update your .env file with your database details:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

Then, run the migration command to set up the default tables:

php artisan migrate

 

Step 3: Create a File Upload Controller

Run the following command to generate a new controller:

php artisan make:controller FileUploadController

Open app/Http/Controllers/FileUploadController.php and update it as follows:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class FileUploadController extends Controller
{
    public function create()
    {
        return view('file-upload');
    }

    public function store(Request $request)
    {
        // Validate files
        $validator = Validator::make($request->all(), [
            'files.*' => 'required|mimes:jpg,jpeg,png,pdf|max:2048',
        ]);
        
        if ($validator->fails()) {
            return back()->withErrors($validator)->withInput();
        }
        
        if ($request->hasFile('files')) {
            foreach ($request->file('files') as $file) {
                $file->store('uploads', 'public');
            }
        }
        
        return back()->with('success', 'Files uploaded successfully!');
    }
}

 

Step 4: Create Routes

Add the following routes in routes/web.php:

use App\Http\Controllers\FileUploadController;

Route::get('/file-upload', [FileUploadController::class, 'create']);
Route::post('/file-upload', [FileUploadController::class, 'store'])->name('file.upload');

 

Step 5: Create a Blade View

Create a new file resources/views/file-upload.blade.php and add the following HTML and Blade code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laravel 12 Multiple File Upload</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
    <h2 class="mb-4">Upload Multiple Files</h2>
    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif
    @if($errors->any())
        <div class="alert alert-danger">
            <ul>
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    <form action="{{ route('file.upload') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <div class="mb-3">
            <label for="files" class="form-label">Choose Files</label>
            <input type="file" name="files[]" class="form-control" multiple>
        </div>
        <button type="submit" class="btn btn-primary">Upload</button>
    </form>
</div>
</body>
</html>

 

Step 6: Run the Application

Now, you can run the Laravel development server:

php artisan serve

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS