无法从 Laravel 5.4 中的存储文件夹下载文件 [英] Cannot download file from storage folder in laravel 5.4
问题描述
我已通过 $path=$request->file->store('files')
将文件存储在 storage/app/files 文件夹中并保存路径files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc"它在表的列名file
.
I have store the file in storage/app/files folder by $path=$request->file->store('files')
and save the path "files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc" it in a table's column name file
.
我还通过 php artisan storage:link
将存储文件夹链接到公共.
I have also linked storage folder to public through php artisan storage:link
.
在我的视图刀片文件中,我把这个
In my view blade file, I put this
<a href="@if(count($personal_information)) {{asset('storage/'.$personal_information->file)}} @endif" download>Download File</a>
下载文件的链接是http://localhost:8000/storage/files/LKaOlKhE5uITzAbRj5PkkNunWldmUTm3tOWPfLxO.doc
但我收到错误
RouteCollection.php 第 161 行中的 NotFoundHttpException
NotFoundHttpException in RouteCollection.php line 161
如果我在 /storage
之后添加 /app
它会给出相同的错误.如何从我的 storage/app/files
文件夹下载文件?
If I add /app
after the /storage
it gives the same error. How can I download file from my storage/app/files
folder?
推荐答案
问题是 storage 文件夹在默认情况下不可公开访问.存储文件夹很可能用于保存一些其他用户无法访问的私人文件,例如用户图片.如果您将它们移动到公用文件夹,则每个人都可以访问文件.我在 Laravel 5.4 上遇到了类似的问题,我通过编写下载文件的路径做了一些小改动.
Problem is storage folder is not publicly accessible in default. Storage folder is most likely forsave some private files such as users pictures which is not accessible by other users. If you move them to public folder files will be accessible for everyone. I had similar issue with Laravel 5.4 and I did a small go around by writing a route to download files.
Route::get('files/{file_name}', function($file_name = null)
{
$path = storage_path().'/'.'app'.'/files/'.$file_name;
if (file_exists($path)) {
return Response::download($path);
}
});
或者您可以将文件保存到 public 文件夹中.
Or you can save your files into public folder up to you.
这篇关于无法从 Laravel 5.4 中的存储文件夹下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!