Laravel 11 Image Upload Example Tutorial

By Hardik Savani April 16, 2024 Category : Laravel

Hi Dev, In this article, I will show you how to image upload in laravel 11 application.

In this tutorial, we will create two routes: one for the GET method to render forms and another for the POST method to upload image code. We created a simple form with a file input. So, you have to simply select an image, and then it will upload in the "images" directory of the public folder.

So, you simply have to follow the below steps to upload an image in a Laravel 11 application.

Laravel 11 Image Upload

Step for Laravel 11 Image Upload Example

  • Step 1: Install Laravel 11
  • Step 2: Create Controller
  • Step 3: Create and Add Routes
  • Step 4: Create Blade File
  • Run Laravel App

Step 1: Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Controller

In this step, we will create a new `ImageController`. In this file, we will add two methods: `index()` and `store()`, for rendering views and storing image logic.

Let's create the `ImageController` by following this command:

php artisan make:controller ImageController

Next, let's update the following code to the controller file.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class ImageController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function index(): View
        return view('imageUpload');
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function store(Request $request): RedirectResponse
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        $imageName = time().'.'.$request->image->extension();  
        $request->image->move(public_path('images'), $imageName);
            Write Code Here for
            Store $imageName name in DATABASE from HERE 
        return back()->with('success', 'Image uploaded successfully!')
                     ->with('image', $imageName); 

Store Image in Storage Folder

$request->image->storeAs('images', $imageName);
// storage/app/images/file.png

Store Image in Public Folder

$request->image->move(public_path('images'), $imageName);
// public/images/file.png

Store Image in S3

$request->image->storeAs('images', $imageName, 's3');

Step 3: Create and Add Routes

Furthermore, open `routes/web.php` file and add the routes to manage GET and POST requests for rendering views and storing image logic.


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;
Route::get('image-upload', [ImageController::class, 'index']);
Route::post('image-upload', [ImageController::class, 'store'])->name('');

Step 4: Create Blade File

At the last step, we need to create an "imageUpload.blade.php" file. In this file, we will create a form with a file input button. So, copy the code below and paste it into that file.


<!DOCTYPE html>
    <title>Laravel 11 Image Upload Example -</title>
    <link href="" rel="stylesheet" crossorigin="anonymous">
    <link rel="stylesheet" href="" />
<div class="container">
    <div class="card mt-5">
        <h3 class="card-header p-3"><i class="fa fa-star"></i> Laravel 11 Image Upload Example -</h3>
        <div class="card-body">
                <div class="alert alert-success" role="alert"> 
                    {{ $value }}
                <img src="images/{{ Session::get('image') }}" width="40%">
            <form action="{{ route('') }}" method="POST" enctype="multipart/form-data">
                <div class="mb-3">
                    <label class="form-label" for="inputImage">Image:</label>
                        class="form-control @error('image') is-invalid @enderror">
                        <span class="text-danger">{{ $message }}</span>
                <div class="mb-3">
                    <button type="submit" class="btn btn-success"><i class="fa fa-save"></i> Upload</button>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:



Laravel 11 Image Upload Output

I hope it can help you...