Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.3: delete image from storage

I have a method which deletes a product and image that belongs to it, but I can not delete the image.

Where am I going wrong with this?

public function deleteProduct($ids)
{
    foreach ($ids as $id => $value) {
        $product = Product::find($id);
        $productImage = $product->image_path;
        if ($productImage) {
            Storage::delete($productImage);
            $product->destroy($id);
        }
    }
}

I have a symlink between storage and public folder.

Under storage images are located at e.g. - storage/app/public/images/products/4/image.png

Under public - public/storage/images/products/4/imagep.png

Image path is stored in database as - /storage/images/products/4/ACTCMRcYlWR8Bn3ZxoJ7bpiDJ7.png

like image 623
rits Avatar asked Apr 30 '17 12:04

rits


People also ask

How do I delete files from Storage in Laravel?

One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.

Can I delete Storage folder in Laravel?

To delete a directory in the Laravel application you can make use of the "deleteDirectory()" method available from the Storage facade. <? php Storage::deleteDirectory($directory); This method will delete the directory with all of its files so you must carefully ensure that you are deleting the right directory.

How do I delete multiple pictures in Laravel?

use Illuminate\Support\Facades\Storage; $images = explode(",", $vehicles->images); Storage::delete($iamges); Both of these examples can be found in the Laravel docs. Yes, your code still adds images to the uploaded-images folder. First you are deleting the existing images, then you are adding the new ones.


1 Answers

I had to add remove '/storage' part of the path and prepend '/public'.

$productImage = str_replace('/storage', '', $product->image_path);

Storage::delete('/public' . $productImage);

like image 188
rits Avatar answered Nov 15 '22 05:11

rits