In this tutorial we will discuss how to delete image from folder in laravel. Image delete not working in laravel. unlink is not working in laravel. Delete a file in laravel.
Delete image from public folder
When you want to delete an image or file first check whether it exists or not then delete it by the laravel file delete method.
App/Http/Controllers/ProductController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use File; class ProductController extends Model { public function imageDelete() { $image_name = 'demo.png'; $image_path = public_path('products/images/'.$image_name); if(File::exists($image_path)) { File::delete($image_path); } } }
Read Also : Unset an item in laravel collection
File delete by php unlink method
You can also delete image by php unlink method.
App/Http/Controllers/ProductController.php<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Model { public function imageDelete() { $image_name = 'demo.png'; $image_path = public_path('products/images/'.$image_name); if(file_exists($image_path)){ unlink($image_path); } } }
Common error in laravel file delete
Some time we have erros in image delete then first check folder permission(Read/Write) or give image proper path.