【问题标题】:Unable to retrieve the file_size for file on downloading storage file - Laravel下载存储文件时无法检索文件的 file_size - Laravel
【发布时间】:2023-02-25 05:40:45
【问题描述】:

只是一个简单的问题。

尝试下载存储文件,但我无法从存储文件夹中下载文件。

在谷歌上搜索。找到两种下载存储文件的方法。两者都试过了,但都是徒劳的。

return response()->download(storage_path("app\public\uploads\1662531990_Dropshipping.docx"));
return Storage::disk('public')->download("app\public\uploads\1662531990_Dropshipping.docx", "1662531990_Dropshipping");

是的,上面给定的文件存在于给定的路径中。下面是该目录和错误屏幕的屏幕截图。

它与 flysystem 有关,因此下面是相关的包,我认为是 composer show 的结果

league/config                      v1.1.1  Define configuration arrays with strict schemas an...   
league/flysystem                   3.2.1   File storage abstraction for PHP
league/flysystem-aws-s3-v3         3.0.0   AWS S3 filesystem adapter for Flysystem.
league/mime-type-detection         1.11.0  Mime-type detection for Flysystem

【问题讨论】:

  • 检查是否安装并启用了fileinfo。大部分时间都是这样,但请务必输入phpinfo()。还要确保该文件可由网络服务器读取
  • 是否创建了存储/公共符号链接?
  • 你解决了吗?我在尝试使用 return Storage::disk('pdf')->download(storage_path('app/pathToThe/'.$zip_file)); 下载 .zip 文件时遇到完全相同的错误

标签: php laravel path storage flysystem


【解决方案1】:

从 Laravel 9 的存储中下载文件的最简单方法是使用 documentation 中所示的下载方法。

return Storage::download('file.jpg');
 
return Storage::download('file.jpg', $name, $headers);

您将在下面找到我用来下载 pdf 的代码示例:

public function download(Publication $publication){
    //Path from the local storage directory (./storage/app/)
    $path = $publication->file_path;
    
    //Name of the file the user will see
    $slug = Str::slug($publication->title).'.pdf';

    $headers = [
        'Content-Type' => 'application/pdf',
     ];

     return Storage::download($path, $slug, $headers);
}

希望这可以帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多