We almost require to file upload or image upload function in our laravel application. We simply make image upload functionality and put that image om our upload directory. But when record will delete from database then it should be remove from folder too otherwise there are several useless images store.
So, in this article i will let you know how to check file exists or not then delete image or file from folder. So you can also do this using Laravel File System and using core php function file_exists() and unlink(). I going to give you both example, you can use any one as you require.
Using File System:
public function removeImage()
{
if(\File::exists(public_path('upload/bio.png'))){
\File::delete(public_path('upload/bio.png'));
}else{
dd('File does not exists.');
}
}
Using Core PHP:
public function removeImage()
{
if(file_exists(public_path('upload/bio.png'))){
unlink(public_path('upload/bio.png'));
}else{
dd('File does not exists.');
}
}
I hope you find your solution....
Do you like below Tutorials ?
- How to Open URL in New Tab using Jquery?
- Laravel 7.x and 6.x Routing Tutorial
- Ng Bootstrap Modal in Angular 8 Example
- Bootstrap 4 Datepicker in Angular 9/8 Example
- Disable Registration Route in Laravel
- Bootstrap Timepicker in Angular Example
- Count Number of Pages in PDF - PHP Script
- Digital Signature PHP Script Example