In this article, we'll see how to store images in a storage folder in laravel 10. Here, we'll learn about uploading images in a storage folder in laravel 10. Together, we'll explore the creation of an image upload form, and tackle image validation.
Learn to store images neatly in a designated storage folder, and even conquer the task of displaying them on our website.
Laravel 10 Image Store in Storage Folder
If you don't have a Laravel project already set up, you can create one using Composer.
composer create-project --prefer-dist laravel/laravel laravel_image_upload
cd laravel_image_upload
Create a form in one of your views to allow users to upload images. For example, you can create a form in resources/views/upload.blade.php
:
<h3>How to Store Image in Storage Folder in Laravel 10 - Vidvatek</h3>
<form action="{{ route('upload') }}" method="POST" enctype="multipart/form-data">
@csrf
<input type="file" name="image" accept="image/*">
<button type="submit">Upload Image</button>
</form>
Generate a controller to handle the image upload logic.
php artisan make:controller ImageUploadController
In the ImageUploadController
, you can create a method to handle the image upload.
use Illuminate\Http\Request;
class ImageUploadController extends Controller
{
public function index()
{
return view('upload');
}
public function upload(Request $request)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', // Adjust validation rules as needed
]);
$imagePath = $request->file('image')->store('images', 'public');
return back()
->with('success', 'Image uploaded successfully.')
->with('imagePath', $imagePath);
}
}
In the routes/web.php
file, define the routes for the image upload form and processing.
Route::get('/upload', 'ImageUploadController@index');
Route::post('/upload', 'ImageUploadController@upload')->name('upload');
To make uploaded images accessible from the public directory, create a symbolic link.
php artisan storage:link
You can display the uploaded images in your views using the symbolic link you created, for example, in a view.
@if (session('imagePath'))
<img src="{{ asset('storage/' . session('imagePath')) }}" alt="Uploaded Image">
@endif
Now, run the laravel 10 application using the following command.
php artisan serve
You might also like:
- Read Also: How To Export CSV File In Laravel 10 Example
- Read Also: Laravel 10 CKeditor Image Upload Example
- Read Also: How to Add Watermark on Image using Laravel 10
- Read Also: How to Create CRUD with Image Upload in Laravel 10