问题
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
localdriver, all file operations are relative to therootdirectory defined in yourfilesystemsconfiguration file. By default, this value is set to thestorage/appdirectory.
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