首页 > 解决方案 > 在 Laravel 7 中找不到文件异常

问题描述

我正在开发一个 laravel 应用程序,我想通过单击按钮下载文件。我已将文件存储在/storage/app/public/1.pdf其中并创建了指向该public文件夹的符号链接。现在我尝试了很多方法来下载文件,但每次都收到错误消息File Not Fount Exception at path

我尝试了以下下载文件的方法:

1 -
$name = "file.pdf";
$file = storage_path(). "/app/public/1.pdf";
$headers = array(
'Content-Type: application/pdf');
return Storage::download($file, $name, $headers);

2 - 
$name = "file.pdf";
    $file = public_path(). "/storage/1.pdf";
    $headers = array(
          'Content-Type: application/pdf',
        );

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


3 - 
$name = "file.pdf";
    $file = Storage::url("1.pdf");
    $headers = array(
          'Content-Type: application/pdf',
        );

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

这是我收到的错误消息:

在此处输入图像描述

在此处输入图像描述

我尝试了很多次,但对我没有任何帮助。提前感谢任何帮助。

标签: laraveldownload

解决方案


来自文档:
本地驱动程序

使用local驱动程序时,所有文件操作都相对于配置文件root中定义的目录。filesystems默认情况下,此值设置为storage/app目录。

因此,如果文件存储在 中storage/app/public/1.pdf,请不要将这样的绝对路径传递给 Storage 门面:

$file = storage_path(). "/app/public/1.pdf";
return Storage::download($file);

而是使用相对于配置文件root中定义的目录的路径:filesystems

$file = "public/1.pdf";
$name = "file.pdf";
return Storage::download($file, $name);

推荐阅读