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

I want to make multiple foreign keys in Laravel, can I?
Publish date: 2022-01-27 | Comments: 5
How can I use arabic words, paragraphs in the faker?
Publish date: 2022-03-06 | Comments: 1
Laravel redirect to the last location after login
Publish date: 2022-02-21 | Comments: 1
Uncaught Error: Missing required parameters: sitekey Laravel
Publish date: 2022-01-08 | Comments: 2
laravel installation error with composer install
Publish date: 2022-02-23 | Comments: 2