如何通过URL地址从存储中打开文件? [英] How to open file from storage by URL address?
问题描述
我将文件存储在存储中.
I store files in storage.
否,我没有尝试生成打开文件的链接,例如:
No I tried to generate link that open file like:
http://[temp.com]/storage/tests/de139607636857fade861a3c2c472643.txt
但是它不起作用.
如何通过URL地址从存储中打开文件?
How to open file from storage by URL address?
推荐答案
默认情况下,无法通过url访问存储中的文件
Files in the storage are not accessible by url by default
您可以使用公共磁盘.为此,您需要创建从public/storage
到storage/app/public
You could use Public Disk. For that you need to create symbolic link from public/storage
to storage/app/public
从Larvel 5.3及更高版本开始,您将拥有artisan命令,可帮助您创建符号链接
From Larvel 5.3 and up, you have artisan command that will help you to create symlink
php artisan storage:link
如果您使用的是Laravel的旧版本,则可以找到有关如何创建符号链接的答案
If you are using older version of Laravel, you can find an answer how to create symbolic link here
另一种方法是通过编辑文件config/filesystems.php
Another approach would be to create new disk "uploads", by editing the file config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
要将文件存储在此位置
Storage::disk('uploads')->put($file_name, $file_content);
并获取文件
asset('uploads/'. $file_name)
这篇关于如何通过URL地址从存储中打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!