简体   繁体   中英

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

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 :)

You can use the normal PHP delete file keyword ( @unlink )

$image_path = "the name of your image path here/".$request->Image; 

 if (file_exists($image_path)) {

       @unlink($image_path);

   }

This is what I do to delete an image:

public function SliderDelete(String $slider_id)
{
    $slider             = Slider::findOrFail($slider_id);
    $image_path         = public_path("\storage\images\sliders\\") .$slider->photo;

    if(File::exists($image_path)) {
        File::delete($image_path);
    }
    else{
        $slider->delete();
        //abort(404);
    }

    $slider->delete();

    return response()->json(['success'=>'Slider deleted successfully!']);
}
 $filename = public_path($fileloc);

            if(File::exists($filename)) {
                File::delete($filename);
            }

call this function and pass two parameter $filepath = path where your file exist $filename = name of your file

public static function UnlinkImage($filepath,$fileName)
{
    $old_image = $filepath.$fileName;
    if (file_exists($old_image)) {
       @unlink($old_image);
    }
}

I had the same problem today but now the files are deleting by this code

 $image_path = $data->image;  
    if(file_exists($image_path)) {
        File::delete($image_path);
    }

basically the poblem for me was the if condition didn't returned somthing true. and the statement didnt executed.

btw I'm storing my file in

hope this helps someone.

public function destroy($id)
    {   

        $imagePath = YourModelName::select('image')->where('id', $id)->first();

         $filePath = $imagePath->image;

                   if (file_exists($filePath)) {

                   unlink($filePath);

           YourModelName::where('id', $id)->delete();

        }else{

         YourModelName::where('id', $id)->delete();
        }


    }

in laravel 8 you do it like

// import Storage class
use Illuminate\Support\Facades\Storage;

Storage::disk('public')->delete('path-of-file');

you can use disk of your choice like

Storage::disk('s3')->delete('path-of-file');

To be able to delete the image it should be in the following form : "/uploads/img1.jpg" where uploads directory is in the public directory then use the following code:

$image_path = puplic_path("/uploads/img1.jpg")
if (file_exists($image_path)) {
        File::delete($image_path);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM