File Not Found Exception in Laravel 7

天涯浪子 提交于 2021-02-10 15:46:44

问题


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.


回答1:


From the docs:
The Local Driver

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.

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

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


来源:https://stackoverflow.com/questions/64771381/file-not-found-exception-in-laravel-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!