php Laravel - 获取存储磁盘路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39840324/
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
Laravel - Get storage disk path
提问by Dewan159
I have created a new storage disk for the public uploads. I use it like this:
我为公共上传创建了一个新的存储磁盘。我像这样使用它:
Storage::disk('uploads')->get(...);
But I am trying to figure out a way to get the path to the disk itself, I have spent some considerable time wondering between the framework files, but couldn't find what I am looking for. I want to use it in a scenario like so:
但是我试图找出一种方法来获取磁盘本身的路径,我花了相当多的时间在框架文件之间想知道,但找不到我要找的东西。我想在这样的场景中使用它:
$icon = $request->file('icon')->store('icons', 'uploads');
Image::make(Storage::disk('uploads')->path($icon))->resize(...)->save();
So, how to get a storage disk's path ?
那么,如何获取存储磁盘的路径呢?
回答by Dewan159
After extra search I found that I can use the following:
经过额外的搜索,我发现我可以使用以下内容:
$path = Storage::disk('uploads')->getAdapter()->getPathPrefix();
But still, isn't a "->path()" method is a given here or what!
但是,这里不是给定的“->path()”方法还是什么!
回答by PiTheNumber
There is a function for this!
有一个功能!
Storage::disk('uploads')->path('/');
https://laravel.com/api/5.8/Illuminate/Filesystem/FilesystemAdapter.html#method_path
https://laravel.com/api/5.8/Illuminate/Filesystem/FilesystemAdapter.html#method_path
Looks like this has been there since 5.4! I never noticed...
看起来这从 5.4 开始就存在了!我从来没有注意到...
回答by Jordan H
There are helper functions in the framework now that can get some of the commonly used paths in the app fairly easily.
现在框架中有辅助函数,可以很容易地获取应用程序中的一些常用路径。
https://laravel.com/docs/5.8/helpers
https://laravel.com/docs/5.8/helpers
For example, if you wanted to access a file from the public folder you could do the below.
例如,如果您想访问公共文件夹中的文件,您可以执行以下操作。
$image_file_path = public_path("images/my_image.jpg")
This will return you a local path for a file located in your app under the public/images folder.
这将为您返回位于您的应用程序中 public/images 文件夹下的文件的本地路径。
回答by user8555937
We can also access the disk storage path from configuration helper directly:
我们也可以直接从配置助手访问磁盘存储路径:
config('filesystems.disks');