Laravel 5.3:从存储中删除图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43706241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:53:31  来源:igfitidea点击:

Laravel 5.3: delete image from storage

laravellaravel-5.3

提问by rits

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

下存储图像位于例如 - storage/app/public/images/products/4/image.png

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

在公共下 - public/storage/images/products/4/imagep.png

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

图像路径在数据库中存储为 - /storage/images/products/4/ACTCMRcYlWR8Bn3ZxoJ7bpiDJ7.png

采纳答案by rits

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

我必须添加删除路径的“/storage”部分并添加“/public”。

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

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

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

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