php 如何在laravel 5中使用Storage facade获取文件路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28964412/
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 get file path using Storage facade in laravel 5?
提问by daviestar
I've been experimenting using the new Flysystem integration with Laravel 5. I am storing 'localised' paths to the DB, and getting the Storage facade to complete the path. For example I store screenshots/1.jpg
and using
我一直在尝试使用新的 Flysystem 与 Laravel 5 的集成。我将“本地化”路径存储到数据库,并获取存储外观以完成路径。例如我存储screenshots/1.jpg
和使用
Storage::disk('local')->get('screenshots/1.jpg')
or
或者
Storage::disk('s3')->get('screenshots/1.jpg')
I can retrieve the same file on different disks.
我可以在不同的磁盘上检索相同的文件。
get
retrieves the file contents, but I am hoping to use it in my views like this:
get
检索文件内容,但我希望在我的视图中使用它,如下所示:
<img src="{{ Storage::path('screenshots/1.jpg') }}" />
but path, or anything able to retrieve the full path is not available (as far as I can see). So how can I return the full path? Or, I'm wondering if this is by design? If so, why am I not supposed to be able to get the full path? Or, am I going about this completely the wrong way?
但是路径或任何能够检索完整路径的东西都不可用(据我所知)。那么我怎样才能返回完整路径呢?或者,我想知道这是否是设计使然?如果是这样,为什么我不能获得完整路径?或者,我是否完全以错误的方式处理这个问题?
采纳答案by Pere Joan Martorell
Edit: Solution for L5.2+
编辑:L5.2+ 的解决方案
There's a better and more straightforward solution.
有一个更好、更直接的解决方案。
Use Storage::url($filename)
to get the full path/URL of a given file. Note that you need to set S3
as your storage filesystem in config/filesystems.php
: 'default' => 's3'
使用Storage::url($filename)
得到给定文件的完整路径/ URL。请注意,您需要在以下位置设置S3
为您的存储文件系统config/filesystems.php
:'default' => 's3'
Of course, you can also do Storage::disk('s3')->url($filename)
in the same way.
当然,你也可以Storage::disk('s3')->url($filename)
用同样的方法。
As you can see in config/filesystems.php
there's also a parameter 'cloud' => 's3'
defined, that refers to the Cloud filesystem. In case you want to mantain the storage folder in the local server but retrieve/store some files in the cloud use Storage::cloud()
, which also has the same filesystem methods, i.e. Storage::cloud()->url($filename)
.
如您所见,config/filesystems.php
还'cloud' => 's3'
定义了一个参数,它指的是云文件系统。如果您想在本地服务器中维护存储文件夹但在云中检索/存储一些文件,请使用Storage::cloud()
,它也具有相同的文件系统方法,即Storage::cloud()->url($filename)
.
The Laravel documentation doesn't mention this method, but if you want to know more about it you can check its source code here.
Laravel 文档没有提到这个方法,但是如果你想了解更多关于它的信息,你可以在这里查看它的源代码。
回答by shock_gone_wild
The Path to your Storage disk would be :
存储磁盘的路径为:
$storagePath = Storage::disk('local')->getDriver()->getAdapter()->getPathPrefix()
I don't know any shorter solutions to that...
我不知道任何更短的解决方案......
You could share the $storagePath to your Views and then just call
您可以将 $storagePath 共享到您的视图,然后调用
$storagePath."/myImg.jpg";
回答by Bilal Gultekin
This method exists since Laravel 5.4, you can get it by:
这个方法从 Laravel 5.4 开始就存在,你可以通过以下方式获得它:
$path = Storage::disk('public')->path($filename);
回答by daviestar
This is how I got it to work - switching between s3 and local directory paths with an environment variable, passing the path to all views.
这就是我让它工作的方式 - 使用环境变量在 s3 和本地目录路径之间切换,将路径传递给所有视图。
In .env:
在 .env 中:
APP_FILESYSTEM=local or s3
S3_BUCKET=BucketID
In config/filesystems.php:
在 config/filesystems.php 中:
'default' => env('APP_FILESYSTEM'),
In app/Providers/AppServiceProvider:
在 app/Providers/AppServiceProvider 中:
public function boot()
{
view()->share('dynamic_storage', $this->storagePath());
}
protected function storagePath()
{
if(Storage::getDefaultDriver() == 's3') {
return Storage::getDriver()
->getAdapter()
->getClient()
->getObjectUrl(env('S3_BUCKET'), '');
}
return URL::to('/');
}
回答by filius
First get file url/link then path, as below:
首先获取文件 url/link 然后是路径,如下:
$url = Storage::disk('public')->url($filename);
$path = public_path($url);
回答by fsasvari
If you just want to display storage (disk) path use this:
如果您只想显示存储(磁盘)路径,请使用以下命令:
Storage::disk('local')->url('screenshots/1.jpg'); // storage/screenshots/1.jpg
Storage::disk('local')->url(''): // storage
Also, if you are interested, I created a package (https://github.com/fsasvari/laravel-uploadify) just for Laravel so you can use all those fields on Eloquent model fields:
另外,如果您有兴趣,我为 Laravel创建了一个包(https://github.com/fsasvari/laravel-uploadify),这样您就可以在 Eloquent 模型字段上使用所有这些字段:
$car = Car::first();
$car->upload_cover_image->url();
$car->upload_cover_image->name();
$car->upload_cover_image->basename();
$car->upload_cover_image->extension();
$car->upload_cover_image->filesize();
回答by manix
Well, weeks ago I made a very similiar question (Get CDN url from uploaded file via Storage): I wanted the CDN url to show the image in my view (as you are requiring ).
好吧,几周前我提出了一个非常相似的问题(通过存储从上传的文件中获取 CDN 网址):我希望 CDN 网址在我的视图中显示图像(如您所需要)。
However, after review the package API I confirmed that there is no way do this task. So, my solution was avoid using flysystem
. In my case, I needed to play with RackSpace. So, finally decide to create my use package and make my own storage package using The PHP SDK for OpenStack.
但是,在查看包 API 后,我确认无法执行此任务。所以,我的解决方案是避免使用flysystem
. 就我而言,我需要玩 RackSpace。所以,最终决定使用The PHP SDK for OpenStack创建我的使用包并制作我自己的存储包。
By this way, you have full access for functions that you need like getPublicUrl()
in order to get the public URL from a cdn container:
通过这种方式,您可以完全访问您需要的功能,getPublicUrl()
以便从 cdn 容器中获取公共 URL:
/** @var DataObject $file */
$file = \OpenCloud::container('cdn')->getObject('screenshots/1.jpg');
// $url: https://d224d291-8316ade.ssl.cf1.rackcdn.com/screenshots/1.jpg
$url = (string) $file->getPublicUrl(UrlType::SSL);
In conclusion, if need to take storage service to another level, then flysystem
is not enough. For local
purposes, you can try @nXu's solution
总之,如果需要将存储服务提升到另一个层次,那flysystem
是不够的。出于local
目的,您可以尝试@nXu 的解决方案
回答by Arvind Bhardwaj
If you need absolute URL of the file, use below code:
如果您需要文件的绝对 URL,请使用以下代码:
$file_path = \Storage::url($filename);
$url = asset($file_path);
// Output: http://example.com/storage/filename.jpg
回答by Marc Garcia
Another solution I found is this:
我发现的另一个解决方案是:
Storage::disk('documents')->getDriver()->getConfig()->get('url')
Will return the url with the base path of the documents Storage
将返回带有文档存储基本路径的 url
回答by Mevlüt?zdemir
Store method:
存储方法:
public function upload($img){
$filename = Carbon::now() . '-' . $img->getClientOriginalName();
return Storage::put($filename, File::get($img)) ? $filename : '';
}
Route:
路线:
Route::get('image/{filename}', [
'as' => 'product.image',
'uses' => 'ProductController@getImage',
]);
Controller:
控制器:
public function getImage($filename)
{
$file = Storage::get($filename);
return new Response($file, 200);
}
View:
看法:
<img src="{{ route('product.image', ['filename' => $yourImageName]) }}" alt="your image"/>