简体   繁体   English

有没有办法在 PHP 中的 mkdir() 或 chmod() 中设置字符串权限模式?

[英]Is there a way to set a string permission mode in mkdir() or chmod() in PHP?

When I looked up the PHP Manual for mkdir() and chmod(), it appears that both functions expect an integer value (eg mkdir( 'a/dir/path', 0700, false ); ).当我查找 mkdir() 和 chmod() 的 PHP 手册时,似乎这两个函数都需要一个整数值(例如mkdir( 'a/dir/path', 0700, false ); )。 I did see that there are other modes I can use such as inval() or ocdec() on the mode parameter, so I'm wondering... is there something like that for strings?我确实看到我可以在 mode 参数上使用其他模式,例如 inval() 或 ocdec(),所以我想知道......字符串是否有类似的东西?

For example, mkdir( 'a/dir/path', strval( 'u+rwx' ), false );例如, mkdir( 'a/dir/path', strval( 'u+rwx' ), false ); . . The reason for this is so that when other people who are (also) not as experienced in PHP read my code, it will be more apparent what permissions I'm setting.这样做的原因是当其他(也)没有 PHP 经验的人阅读我的代码时,我设置的权限会更加明显。

First of all, I don't think that it is strictly necessary to implement that kind of function: numeric permissions are actually intuitive for those who know how to read them.首先,我不认为实现那种功能是绝对必要的:对于那些知道如何阅读它们的人来说,数字权限实际上是直观的。

However, to answer the question, to convert a string like " -rwxr-xrw- " you could use something like this function:但是,要回答这个问题,要转换像“ -rwxr-xrw- ”这样的字符串,您可以使用以下函数:

NB: you should REALLY add some input validation to the function below (check string length, valid chars, etc..)注意:您真的应该为下面的函数添加一些输入验证(检查字符串长度、有效字符等。)

function format($permissions)
{
    //Initialize the string that will contain the parsed perms.
    $parsedPermissions = "";

    //Each char represents a numeric constant that is being added to the total
    $permissionsDef = array(
        "r" => 4,
        "w" => 2,
        "x" => 1,
        "-" => 0
    );

    //We cut the first of the 10 letters string
    $permissions = substr($permissions, 1);

    //We iterate each char
    $permissions = str_split($permissions);
    $length = count($permissions);

    $group = 0;

    for ($i = 0, $j = 0; $i < $length; $i++, $j++) {
        if ($j > 2) {
            $parsedPermissions .= $group;

            $j = 0;
            $group = 0;
        }
        $group += $permissionsDef[$permissions[$i]];
    }

    $parsedPermissions .= $group;

    return $parsedPermissions;
}

AFAIK there is no inbuilt way of doing it. AFAIK 没有内置的方法。 I also do not think there is a need for it.我也不认为有必要这样做。 A link to an explanation of file permissions and a comment such as this one fromhttp://www.onlamp.com/pub/a/php/2003/02/06/php_foundations.html should suffice:指向文件权限解释的链接和来自http://www.onlamp.com/pub/a/php/2003/02/06/php_foundations.html的评论就足够了:

 Value Permission Level -------------------------- 400 Owner Read 200 Owner Write 100 Owner Execute 40 Group Read 20 Group Write 10 Group Execute 4 Global Read 2 Global Write 1 Global Execute Permission Calculations: ------------------------ 400 Owner Read + 100 Owner Execute + 20 Group Write + 4 Global Read ----------------------------- = 0524 Total Permission Value

This is easier than writing a function to correctly parse all the possible strings that can be used as file permissions.这比编写一个函数来正确解析可用作文件权限的所有可能字符串更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM