Greetings developers, In this article, we will explore the process of image uploading in Laravel 11. Additionally, we'll delve into techniques for validating both the image file type and size within the Laravel 11 framework.
I'm excited to share with you a straightforward example of how to upload images in Laravel 11. Throughout this guide, we'll walk through the steps together, making the process easy to understand.
So, let's see laravel 11 image upload, how to validate image type in laravel 11, and how to validate image size in laravel 11.
In the first step, we'll install laravel using the following composer command.
composer create-project laravel/laravel laravel-11-example
Now, we'll create the ImageController. In this controller, we'll add two functions. So, execute the following command.
php artisan make:controller ImageController
app/Http/Controllers/ImageController.php
<?php
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('image-upload');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request): RedirectResponse
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,svg|max:4096',
]);
$image_name = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $image_name);
return back()->with('success', 'Image uploaded successfully!')
->with('image', $imageName);
}
}
Note: create an images folder in the public folder.
Store Image in Storage Folder
$request->image->storeAs('images', $image_name);
// storage/app/images/test.png
Store Image in Public Folder
$request->image->move(public_path('images'), $image_name);
// public/images/test.png
Store Image in S3
$request->image->storeAs('images', $image_name, 's3');
Then, we'll define routes for displaying and storing images in laravel 11. So, open the web.php file.
routes/web.php
<?php
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('image.store');
Create a new Blade file named image-upload.blade.php
. This file will contain the HTML markup for the image upload form.
resources/views/image-upload.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How to Image Upload in Laravel 11 Example - 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 Image Upload in Laravel 11 Example - Techsolutionstuff</h3>
<div class="card-body">
@session('success')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
<img src="images/{{ Session::get('image') }}" width="40%">
@endsession
<form action="{{ route('image.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label class="form-label" for="image">Image:</label>
<input
type="file"
name="image"
id="image"
class="form-control @error('image') is-invalid @enderror">
@error('image')
<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 the last step, execute the following command to start the Laravel development server.
php artisan serve
You might also like: