Laravel 5.3: 从存储中删除图像

Laravel 5.3: delete image from storage

我有一个方法可以删除属于它的产品和图像,但我不能删除图像。

我哪里出错了?

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);
        }
    }
}

我在存储和 public 文件夹之间有一个符号链接。

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

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

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

我必须添加移除路径的“/storage”部分并在前面添加“/public”。

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

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

我会在我的模型或助手中写这样的东西,并在我需要的时候调用它:

public static function delete_file($path)
{
    if (\Storage::disk('public')->exists($path))
        \Storage::disk('public')->delete($path);
}

如果您将 /storage 部分添加到您可以使用文件系统的路径:

if (File::exists(public_path($image->path))) {
            File::delete(public_path($image->path));
}