laravel - 在 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);
这是我收到的错误消息:
我尝试了很多次,但对我没有任何帮助。提前感谢任何帮助。
解决方案
来自文档:
本地驱动程序
使用
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);
推荐阅读
- node.js - Docker Jenkins Pipeline 安装全局 NPM 包
- assembly - [ELF Header] 和 [data segment] 之间的空格是多少?
- ruby-on-rails - 使用omni + desive authenication使用google_oauth2登录时遇到麻烦
- javascript - Async Await 和 Promises - 如何改进语法
- sql-server - 加入 3 个表和组主题得到重复的结果
- c# - 避免 if-else 阶梯中的重复条件
- typescript - Docker 映像不运行 - 找不到模块
- git - 如何使用 git 将整个项目添加到暂存中?
- javascript - 如何在重定向到下一页之前验证输入字段
- python - 为什么我收到错误消息:“FileExistsError: [WinError 183] 当文件已存在时无法创建文件”?