How to remove file if exists from folder in Laravel 5?


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....


July 6, 2017 | 574 Viewer | Category : PHP Laravel

Related Posts: