Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Delete Images from Public/Images Folder in laravel 5 (URL Data)

how to delete images file from public/images folder in laravel 5 ??

i found some example from this site, but i know they are just using the file name in their record table, but i'm using something like URL e.g localhost/project/uploads/filename.jpg on my record table. so if i tried like this :

    $image_path = $data->image;  // the value is : localhost/project/image/filename.format
    if(File::exists($image_path)) {
        File::delete($image_path);
    }

the file is not deleted

help pls, thanks

like image 811
DarkCode999 Avatar asked Nov 28 '22 00:11

DarkCode999


1 Answers

If you want to delete image from your server, you have to reference location of file in directory server, means you could not reference by url link to delete it.

Commonly, Laravel 5 file is locate in public folder.

Example: your files are located in public/images

$image_path = "/images/filename.ext";  // Value is not URL but directory file path
if(File::exists($image_path)) {
    File::delete($image_path);
}

If I can delete image from server by reference URL then Google is the first target :)

like image 90
Sovary Avatar answered May 25 '23 20:05

Sovary