laravel 如何通过URL地址从存储中打开文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46798653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open file from storage by URL address?
提问by OPV
I store files in storage.
我将文件存储在存储中。
No I tried to generate link that open file like:
不,我试图生成打开文件的链接,例如:
http://[temp.com]/storage/tests/de139607636857fade861a3c2c472643.txt
But it does not work.
但它不起作用。
How to open file from storage by URL address?
如何通过URL地址从存储中打开文件?
回答by ljubadr
Files in the storage are not accessible by url by default
默认情况下无法通过 url 访问存储中的文件
You could use Public Disk. For that you need to create symbolic link from public/storage
to storage/app/public
您可以使用公共磁盘。为此,您需要从public/storage
到创建符号链接storage/app/public
From Larvel 5.3 and up, you have artisan command that will help you to create symlink
从 Larvel 5.3 及更高版本开始,您可以使用 artisan 命令来帮助您创建符号链接
php artisan storage:link
php artisan storage:link
If you are using older version of Laravel, you can find an answer how to create symbolic link here
如果您使用的是旧版本的 Laravel,您可以在此处找到如何创建符号链接的答案
Another approach would be to create new disk "uploads", by editing the file config/filesystems.php
另一种方法是通过编辑文件来创建新的磁盘“上传” config/filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path(),
],
'uploads' => [
'driver' => 'local',
'root' => public_path() . '/uploads',
],
]
To store files in this location
将文件存储在此位置
Storage::disk('uploads')->put($file_name, $file_content);
And to get the file
并获取文件
asset('uploads/'. $file_name)
回答by ceejayoz
The storage
directory exists outside the web root, as some of the stuff in there shouldn't necessarily be publicly accessible.
该storage
目录存在于 Web 根目录之外,因为其中的某些内容不一定可以公开访问。
https://laravel.com/docs/5.5/filesystem#the-public-disk
https://laravel.com/docs/5.5/filesystem#the-public-disk
The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in
storage/app/public
. To make them accessible from the web, you should create a symbolic link frompublic/storage
tostorage/app/public
. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.
公共磁盘用于将要公开访问的文件。默认情况下,公共磁盘使用本地驱动程序并将这些文件存储在
storage/app/public
. 要使它们可以从 Web 访问,您应该创建一个从public/storage
到的符号链接storage/app/public
。该约定将您的可公开访问的文件保存在一个目录中,当使用 Envoyer 等零停机时间部署系统时,该目录可以轻松地在部署之间共享。
回答by Lloople
I strongly recommend not to modify the Laravel's directory structure.
我强烈建议不要修改 Laravel 的目录结构。
What I would do is to create a URL to download the file, for example:
我要做的是创建一个 URL 来下载文件,例如:
Route::get('download/{file}', 'DownloadsController@index');
And in that method's controller, put some logic to assert that the file exists and serve it with a response download, for example
在该方法的控制器中,放置一些逻辑来断言文件存在并通过响应下载为其提供服务,例如
public function index($file)
{
$filePath = storage_path('tests/'.$file);
if (! file_exists($filePath)) {
// Some response with error message...
}
return response()->download($filePath);
}
Then you can download your file with a link like this
然后你可以用这样的链接下载你的文件
http://[temp.com]/download/de139607636857fade861a3c2c472643.txt
The controller's way allows you to make some authentication checks before serving the file for example, or to count how many times the file was downloaded.
例如,控制器的方式允许您在提供文件之前进行一些身份验证检查,或者计算文件被下载的次数。