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 ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example