PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] Laravel 5.3: delete image from storage

 March 18, 2022     laravel, laravel-5.3     No comments   

Issue

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


Solution

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

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

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



Answered By - rits
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing