How do you get the path to the Laravel Storage folder?
Solution 1
In Laravel 3, call path('storage')
.
In Laravel 4, use the storage_path()
helper function.
Solution 2
For Laravel 5.x, use $storage_path = storage_path()
.
From the Laravel 5.0 docs:
storage_path
Get the fully qualified path to the
storage
directory.
Note also that, for Laravel 5.1 and above, per the Laravel 5.1 docs:
You may also use the
storage_path
function to generate a fully qualified path to a given file relative to the storage directory:$path = storage_path('app/file.txt');
Solution 3
For Laravel version >=5.1
storage_path()
The storage_path
function returns the fully qualified path to the storage directory:
$path = storage_path();
You may also use the storage_path
function to generate a fully qualified path to a given file relative to the storage directory:
$app_path = storage_path('app');
$file_path = storage_path('app/file.txt');
Source: Laravel Doc
Solution 4
use this artisan command for create shortcut in public folder
php artisan storage:link
Than you will able to access posted img or file
Solution 5
Laravel 8.x <
If the file in a default disk: /storage/app/public/file.jpg, then:
$path = Storage::path('file.jpg');
if the file in a different storage. Ex: /storage/app/storage-dir/sub-dir/file.txt
Storage::disk('storage-dir')->path('sub-dir/file.txt')
And the $path will be "/var/www/project1/storage/app/storage-dir/sub-dir/file.txt"
There are also url()
$path = Storage::disk('storage-dir')->url('sub-dir/file.txt');
This will returns the path like: /storage/sub-dir/file.txt
For the ealier Laravel versions:
storage_path('app/file.txt');
Related videos on Youtube
Garry Pettet
I'm a former UK radiologist who now works full time as a web, desktop and iOS developer.
Updated on January 25, 2022Comments
-
Garry Pettet over 2 years
I want to store uploaded images to my Laravel-based web app in a subdirectory of the Laravel
storage
directory. It's the directory at the same hierarchy level as the 'application' and 'public' directories.Is there a framework method for doing this? I've searched the docs but can't find one.
-
Mark Amery over 5 yearsFor the record, the downvote on this answer is mine. I downvoted it because all the prose is copied and pasted from the docs, but is neither wrapped in quote marks or formatted as a block quotation, which is in violation of our referencing policy. My apologies for only commenting to point this out now; I thought I had left a comment previously, but apparently not. Additionally, I think it would've been more useful to simply add the note about Laravel 5.1 as an edit to my answer (now made) than as a standalone answer in the first place.