Laravel 10 Image Upload With Preview Example

In this article, we will see the laravel 10 image upload with a preview example. Here, we will learn about how to image upload with a preview in laravel 10 and laravel 11. We will also validate the file size and validate file type before uploading in laravel 10 and laravel 11.

Also, we will create an images folder in the public path to store the images. Also, you can store images in the database and display uploaded image previews.

So, let's see how to upload an image in laravel 10/11, laravel 10 image upload and display, and how to image upload with a preview laravel 11.

Step 1: Install Laravel 10/11

In this step, we will install laravel 10 using the following command. So, run the below command.

composer create-project --prefer-dist laravel/laravel laravel_10_image_upload_example



Step 2: Create Controller

In this step, we will create a new ImageController. So, add the following code to that file.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ImageController extends Controller
    public function index()
        return view('index');

    public function store(Request $request)
            'image' => 'required|image|mimes:png,jpg,jpeg,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', 'You have successfully uploaded an image.')->with('image', $imageName); 

Store Image in Storage Folder:

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

// storage/app/images/image_name.png

Store Image in Public Folder:

$request->image->move(public_path('images'), $imageName);

// public/images/image_name.png

Store Image in S3:

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



Step 3: Add Routes

Now, we will add routes in the web.php file.



use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImageController;
| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::get('/', function () {
    return view('welcome');

Route::get('index', [ImageController::class, 'index']);
Route::post('image-upload', [ImageController::class, 'store'])->name('');


Step 4: Create Blade File

In this step, we will create an index.blade.php file. So, add the following HTML code to that file for upload image and display image.

<!DOCTYPE html>
        <title>Laravel 10 Image Upload With Preview - Websolutionstuff</title>
        <link rel="stylesheet" href="">
        <script src=""></script>
        <script src=""></script>
        <div class="container">       
            <div class="panel panel-primary col-md-8">  
                <div class="panel-heading mt-5">
                    <h4>Laravel 10 Image Upload With Preview - Websolutionstuff</h4>
                <div class="panel-body">       
                    @if ($message = Session::get('success'))
                    <div class="alert alert-success alert-dismissible fade show" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                    <img class="mb-3" src="images/{{ Session::get('image') }}" style="width: 250px;">
                    <form action="{{ route('') }}" method="POST" enctype="multipart/form-data">
                        <div class="mb-3">
                            <label class="form-label" for="inputImage">Image:</label>
                            <input type="file" name="image" id="inputImage" 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">Upload</button>




You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of 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.