23

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 ?

1

5 Answers 5

58

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/10.x/Illuminate/Filesystem/FilesystemAdapter.html#method_path

Looks like this has been there since 5.4! I never noticed...

2
  • 4
    For me, this appended "/" to make a double slash when my root was defined with storage_path("location"). It produced path/to/location//. With just an empty string '' as the parameter, it produced a path with a single slash. Is this anyone else's experience? Dec 29, 2020 at 11:30
  • The link is outdated and doesn't work anymore. The following link works for Laravel 10.x: laravel.com/api/10.x/Illuminate/Filesystem/… May 24, 2023 at 9:47
35

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!

3
  • 5
    I wondering where did you find the code?? there's not written in documentation, right? Sep 27, 2018 at 6:36
  • I think it's from Flysystem package
    – ymakux
    Mar 27, 2020 at 11:47
  • 3
    No longer applicable in Laravel 9 however and for that I need a solution as well to replace $this->storageDisk->getAdapter()->setPathPrefix(public_path('/uploads/'.$this->currentProject->id));
    – rhand
    Aug 31, 2022 at 4:16
7

We can also access the disk storage path from configuration helper directly:

config('filesystems.disks');
2
  • This should be the correct answer as the other ones require you to use functions that aren't on the interface.
    – Nick
    Mar 26, 2020 at 22:03
  • Example from this answer: config('filesystems.disks.uploads.root')
    – Pathros
    Jan 19, 2023 at 18:35
4

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.

4

Laravel Default Method

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

Get List of All Directories using disk name

Storage::disk('disk_name')->directories() 
5
  • Please expand your answer and explain why this works Jul 5, 2022 at 4:27
  • Doesn't work, path needs a file name.
    – Hassan
    Sep 25, 2022 at 9:33
  • This code returned only the Disk default path that is defined in the config/filesystems.php Sep 26, 2022 at 10:47
  • Is there a way to return the list (array) of files in that directory?
    – Pathros
    Sep 30, 2022 at 21:32
  • Get ALL Directory list from disk Storage::disk('disk_name')->directories() Oct 3, 2022 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.