【问题标题】:Delete Laravel file from storage/app/public/photos从 storage/app/public/photos 中删除 Laravel 文件
【发布时间】:2020-06-05 02:52:09
【问题描述】:

我正在尝试从 storage/app/public/photos 中删除文件。 我从前端得到的 url 是 http/IP_Address/app_name/public/storage/photos/file_name。我尝试了取消链接和文件:删除,但都不起作用,因为我相信文件路径不正确。我尝试了 basename 和 public_path 但它没有解决它。有什么帮助吗?

$file_path = public_path(basename($fullLinkToPhoto));
if(File::exists($file_path)) File::delete($file_path);

编辑: 下面由 larabee 回答。我使用了存储方法。这是我的最终代码。我使用 basename 从完整的照片链接中获取了文件名,并在 Storage::delete 中使用了该文件名。

$file_name = basename($fullLinkToPhoto);     
if(\Storage::exists('photos/'.$file_name)){
    \Storage::delete('photos/'.$file_name);
}

【问题讨论】:

  • 如果您的文件存储在storage/app/public/photos,请使用Storage::delete('public/photos/filename.ext');

标签: laravel file


【解决方案1】:

有三种不同的方式:

存储/应用程序/公共/

按存储:

  if(\Storage::exists('photos/picture.png')){
    \Storage::delete('photos/picture.png');
  }

按文件系统:

 if(\File::exists(public_path('photos/picture.png'))){
    \File::delete(public_path('photos/picture.png'));
}

通过 php

if(file_exists(public_path('photos/picture.png'))){
  unlink(public_path('photos/picture.png'));
}

您应该使用“存储解决方案”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 2018-12-19
    • 2023-01-03
    • 2019-04-02
    • 2016-07-30
    • 1970-01-01
    • 2021-11-07
    • 2015-01-14
    相关资源
    最近更新 更多