简体   繁体   中英

Laravel - Get storage disk path

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 ?

Update 2022

You can get the path from the config:

config('filesystems.disks.uploads.root')

Anyway, I keep the old answer, as it still works and is not wrong:


There is a function for this!

Storage::disk('uploads')->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...

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!

我们也可以直接从配置助手访问磁盘存储路径:

config('filesystems.disks');

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

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.

Laravel 默认方法

return Storage::disk('disk_name')->path();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM