无法从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?
推荐答案
问题是存储文件夹在默认情况下不可公开访问.存储文件夹很可能会保存一些私人文件,例如其他用户无法访问的用户图片.如果将它们移到公用文件夹,则每个人都可以访问这些文件.我在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);
}
});
或者,您也可以将文件保存到公共文件夹中.
Or you can save your files into public folder up to you.
这篇关于无法从laravel 5.4中的存储文件夹下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!