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 ?
- Angular Use CKEditor Example Tutorial
- How to Use Multiple Select Dropdown in Laravel?
- Angular 9 Image Upload with Crop Example
- Files Upload on Button Click in PHP Dropzone Example
- PHP Dropzone Allow only One File to Upload Example
- PHP Dropzone Allow only Images to Upload Example
- PHP Dropzone Delete Uploaded File Example
- Livewire CRUD Laravel 7 Example