Laravel 7中未找到文件异常 [英] File Not Found Exception in Laravel 7

查看:73
本文介绍了Laravel 7中未找到文件异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个laravel应用程序,我想通过单击一个按钮来下载文件.我已将文件存储在/storage/app/public/1.pdf 中,并创建了指向 public 文件夹的符号链接.现在,我尝试了多种下载文件的方法,但是每次,我都会收到错误路径上的文件未找到异常.

I am working on a laravel application where I want to download a file by clicking a button. I have stored the file in /storage/app/public/1.pdf and have created a symbolic link to the public folder. Now I tried many ways to download the file but every time, I receive error File Not Fount Exception at path.

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

I have tried the following ways for downloading the file:

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

这是我收到的错误消息:

Here is the error message I get:

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

I tried many times but nothing worked for me. Any help is appreciated in advance.

推荐答案

来自文档:
本地驱动程序

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

When using the local driver, all file operations are relative to the root directory defined in your filesystems configuration file. By default, this value is set to the storage/app directory.

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

So, if the file is stored in storage/app/public/1.pdf, do not pass to the Storage facade an absolute path like this one:

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

请使用相对于文件系统配置文件中定义的 root 目录的路径:

Instead use a path relative to the root directory defined in your filesystems configuration file:

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

这篇关于Laravel 7中未找到文件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆