Laravel 5:具有配置权限的 mkdir/Filesystem::makeDirectory

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28962800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:06:16  来源:igfitidea点击:

Laravel 5: mkdir/Filesystem::makeDirectory with permissions from config

phplaravelpermissionsmkdir

提问by Jonathon

I'm running into an issue this morning with FileSystem::makeDirectorywhich directly calls mkdir. I'm trying to create a directory recursively pulling the desired mode out of my config like so:

今天早上我遇到了一个问题FileSystem::makeDirectory,直接调用mkdir. 我正在尝试创建一个目录,以递归方式从我的配置中提取所需的模式,如下所示:

$filesystem->makeDirectory($path, config('permissions.directory'), true);

config/permissions.php

配置/权限.php

<?php

return [
    'directory' => env('PERMISSIONS_DIRECTORY', 0755),
    'file' => env('PERMISSIONS_FILE', 0644)
];

.env

.env

...
PERMISSIONS_DIRECTORY=0775
PERMISSIONS_FILE=0664    

When this is called, the directory is created but the permissions it gets are messed up. It gets something along the lines of dr----Sr-t+. After some research I've come to the conclusion that when I'm passing the value to the mode parameter from my config using config('permissions.directory')the mode is being treated as decimal rather than an octal. So the call to config is likely returning 775which is being passed into the function, rather than 0775.

当这个被调用时,目录被创建,但它获得的权限被弄乱了。它得到一些类似于dr----Sr-t+. 经过一些研究,我得出的结论是,当我使用config('permissions.directory')模式将值从我的配置传递给模式参数时,被视为十进制而不是八进制。因此,对 config 的调用可能会返回775正在传递给函数的内容,而不是0775.

If I remove the call to config, the directory is created with the correct permissions:

如果我删除对 的调用config,则会使用正确的权限创建目录:

    $filesystem->makeDirectory($path, 0775, true);

Does anyone have any idea how to get around this while still being able to store my permissions on my config file?

有没有人知道如何解决这个问题,同时仍然能够将我的权限存储在我的配置文件中?

采纳答案by Anatoliy Arkhipov

It does not work because permissions should be in octal, not in decimal. When you type 0755as number - it is in octal format. When you are trying to use string "0755"- it will be auto converted to decimal 755. And 755 != 0755.

它不起作用,因为权限应该是八进制,而不是十进制。当您输入0755数字时 - 它是八进制格式。当您尝试使用字符串时"0755"- 它会自动转换为十进制755。并且755 != 0755

So, for correctly converting of string to octal number, you should use the intvalfunction:

因此,为了将字符串正确转换为八进制数,您应该使用以下intval函数:

$permissions = intval( config('permissions.directory'), 8 );
$filesystem->makeDirectory($path, $permissions, true);

http://php.net/manual/en/function.intval.php

http://php.net/manual/en/function.intval.php