如何获得 Laravel 存储文件夹的路径?

我想存储上传的图像到我的 Laravel 为基础的网络应用程序在 Laravel storage目录的子目录。它是与“应用程序”和“公共”目录处于同一层次结构级别的目录。

是否有一个框架方法来做到这一点? 我已经搜索了文档,但找不到一个。

213023 次浏览

In Laravel 3, call path('storage').

In Laravel 4, use the storage_path() helper function.

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');

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

You can use the storage_path(); function to get storage folder path.

storage_path(); // Return path like: laravel_app\storage

Suppose you want to save your logfile mylog.log inside Log folder of storage folder. You have to write something like

storage_path() . '/LogFolder/mylog.log'

use this artisan command for create shortcut in public folder

php artisan storage:link

Than you will able to access posted img or file

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');

Issue this command in the console:

php artisan storage:link