user image

Yasen Sayed
Published in : 2022-03-06

How to delete photo from project's public folder before updating data in lumen/laravel?

Laravel

I am trying to delete a photo from my project's public folder before updating another photo. My code looks like this:

 

if(File::exist( $employee->avatar)){ //$employee->avatar looks /uploads/avatars/1646546082.jpg and it exist in folder File::delete($employee->avatar); }

 

This is not working in my case. If I comment on this code then another photo-updated without deleting perfectly. How can I solve the issue!

Comments

Rakshit Date : 2022-03-06

Best answers

34

Best answers

34

If your file is existing to the mentioned directory, try below code, it will work. 

Here, PHP Unlink function will work for you.

$path = public_path()."/<your path to image directory>/".$from_database->image_name;unlink($path);

Try it, It will work for you.

Reference: PHP Unlink function - w3schools.

Eslam Zedan Date : 2022-03-06

Best answers

8

Best answers

8

Try unlink function

$path = public_path()."/pictures/".$from_database->image_name;unlink($path);

 

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

tymon/jwt-auth 0.5.12 requires illuminate/support ~5.0
Publish date: 2022-02-18 | Comments: 2
Increase Sanctum token length in Laravel x9
Publish date: 2022-07-16 | Comments: 2
[solved] All assets in laravel storage returns 404
Publish date: 2021-12-14 | Comments: 2