Laravel 12 File Upload: Step-by-Step Guide

Hi there! In this guide, I’ll show you how to upload an image in Laravel 12 step-by-step. If you’re new to Laravel or just looking for a simple and clear way to handle image uploads, you’re in the right place.

We'll cover everything from setting up the project, creating the upload form, storing images, and displaying them on your website. Don't worry — I'll keep things simple and easy to understand.

So, let's get started and upload your first image in Laravel 12!

Laravel 12 File Upload: Step-by-Step Guide

Laravel 12 File Upload: Step-by-Step Guide

 

Step 1: Install Laravel 12

If you haven’t installed Laravel 12 yet, create a new project using:

laravel new image-upload-tutorial

 

Step 2: Set Up Routes

Open routes/web.php and define the routes for displaying the upload form and handling the submission.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FileUploadController;

Route::get('/upload', [FileUploadController::class, 'showForm']);
Route::post('/upload', [FileUploadController::class, 'uploadFile']);

 

Step 3: Create the Controller

Run the following command to create a controller:

php artisan make:controller FileUploadController

Now, open app/Http/Controllers/FileUploadController.php and update it as follows:

namespace App\Http\Controllers;

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

class FileUploadController extends Controller
{
    public function showForm()
    {
        return view('upload');
    }

    public function uploadFile(Request $request)
    {
        $request->validate([
            'file' => 'required|image|mimes:jpeg,png,jpg,gif,pdf,csv,xls|max:2048',
        ]);

        $fileName = time().'.'.$request->image->extension();
        $request->file->move(public_path('uploads'), $fileName);

        return back()->with('success', 'File uploaded successfully!')->with('file', $fileName);
    }
}

 

Step 4: Create the Blade View

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

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 12 File Upload</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.0/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
    <h2 class="mb-4">Laravel 12 File Upload</h2>
    
    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
        <img src="{{ asset('uploads/' . session('file')) }}" width="300">
    @endif

    <form action="{{ url('/upload') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <div class="mb-3">
            <label class="form-label">Choose File:</label>
            <input type="file" name="file" class="form-control">
            @error('image')
                <div class="text-danger">{{ $message }}</div>
            @enderror
        </div>
        <button type="submit" class="btn btn-primary">Upload</button>
    </form>
</div>
</body>
</html>

 

Step 5: Run the Application

Start your 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