Home > Software engineering >  Deleting a file from storage does not delete an image
Deleting a file from storage does not delete an image

Time:10-18

I'm using Laravel 5.8 and I wanted to delete an image from storage directory.

Basically the images is placed at this dir:

/storage/app/public/laboratories/labs/21/pics

And I tried running this method:

public function removeAzPic($lab,$azpic)
    {
        $destinationPath = storage_path("laboratories/labs/$lab/pics/$azpic");
        
        Storage::delete($destinationPath);

        return redirect()->back();

    }

But the problem is it does not delete the file from storage!

So what's going wrong here? How can I solve this issue?

CodePudding user response:

You can add this function to the helper.php for to delete files/directories recursively:

Function will help you to delete files at the moment. If you want to delete directories change if ( is_dir($full) ) { to if ( is_file($full) ) {


public static function rrmdir($src = null) {

        if(!is_dir($src)) return false;

        $dir = opendir($src);
        while(false !== ( $file = readdir($dir)) ) {
            if (( $file != '.' ) && ( $file != '..' )) {
                $full = $src . '/' . $file;
                if ( is_dir($full) ) {
                    self::rrmdir($full);
                }  else {
                    @unlink($full);
                }
            }
        }
        closedir($dir);
        rmdir($src);

        return true;
    }
  • Related