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

Laravel Network is unreachable stream_socket_client 587
Publish date: 2022-02-16 | Comments: 4
laravel installation error with composer install
Publish date: 2022-02-23 | Comments: 2
I want to make multiple foreign keys in Laravel, can I?
Publish date: 2022-01-27 | Comments: 5
How can i pass data from blade template to vue component?
Publish date: 2022-02-11 | Comments: 1
Get user location (city - country) from his IP address
Publish date: 2022-03-02 | Comments: 2