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



我想在Laravel storage目录的子目录中存储上传的图像到我的基于Laravel的web应用程序。该目录与"application"one_answers"public"目录处于同一层次结构。

是否有一个框架方法来做这件事?

对于Laravel 5。x,使用$storage_path = storage_path()

来自Laravel 5.0文档:

storage_path

获取storage目录的全限定路径

还要注意,对于Laravel 5.1及以上版本,根据Laravel 5.1文档:

您还可以使用storage_path函数生成相对于存储目录的给定文件的完全限定路径:

$path = storage_path('app/file.txt');

在Laravel 3中,调用path('storage')

在Laravel 4中,使用storage_path()辅助函数

对于Laravel版本>=5.1

storage_path ()

storage_path函数返回存储目录的完全限定路径:

$path = storage_path();

您也可以使用storage_path函数来生成给定文件相对于存储目录的完全限定路径:

$app_path = storage_path('app');
$file_path = storage_path('app/file.txt');

来源: Laravel Doc

x & lt;

如果文件在默认磁盘:/storage/app/public/file.jpg,则:

$path = Storage::path('file.jpg');

表示文件在不同的存储中。例:/存储/app/storage-dir/sub-dir/file.txt

Storage::disk('storage-dir')->path('sub-dir/file.txt')

$路径将是"/var/www/project1/storage/app/storage-dir/sub-dir/file.txt"

还有url()

$path = Storage::disk('storage-dir')->url('sub-dir/file.txt');

这将返回路径:/storage/sub-dir/file.txt

对于早期版本的Laravel:storage_path('app/file.txt');

使用这个artisan命令在公用文件夹

创建快捷方式
php artisan storage:link

你将能够访问张贴的图片或文件

您可以使用storage_path();函数获取存储文件夹路径

storage_path(); // Return path like: laravel_appstorage

假设您想将日志文件mylog.log保存在存储文件夹的Log文件夹中。你必须写像

这样的东西

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

最新更新