开发者

Use 0755 as base-8 number

开发者 https://www.devze.com 2023-04-08 08:53 出处:网络
Here is my code: function myChmod($path, $permission, $log) { //this is to overcome umask limitations that mkdir adheres to

Here is my code:

function myChmod($path, $permission, $log)
{
    //this is to overcome umask limitations that mkdir adheres to
    $result = chmod($path, octdec($permission));
    if(!$result) {
        $log->log("Error failed to chmod '$path' to '$permission'. Exiting.开发者_如何学JAVA");
        throw new Exception("Error failed to chmod '$path' to '$permission'. Exiting.");
    }
    return $result;
}

$trml2pdfPath = $c->install_path.'assets/trml2pdf/trml2pdf/trml2pdf.py';
myChmod($trml2pdfPath, 0755, $log);

How do I prevent PHP from changing this base-8 number 0755 to base-10 number 493? I wanna use the chmod function in PHP, but it just keeps changing it to 493.


PHP's chmod() function takes an integer as second parameter. It does not matter whether you pass octal(0755) or dec(493), it's the same number.


myChmod($trml2pdfPath, 0755, $log);
the 0755 here is a number literal. PHP interprets it as the number oct(755)=dec(493), meaning: you don't need any conversion function in myChmod().

$result = chmod($path, $permission);


From http://docs.php.net/chmod

Note that mode is not automatically assumed to be an octal value, so strings (such as "g+w") will not work properly. To ensure the expected operation, you need to prefix mode with a zero (0):

See this example code in php documentation:

<?php
chmod("/somedir/somefile", 755);   // decimal; probably incorrect
chmod("/somedir/somefile", "u+rwx,go+rx"); // string; incorrect
chmod("/somedir/somefile", 0755);  // octal; correct value of mode
?>

So I guess you need not use base_convert, instead you can directly use the octal number.

This should work for you:

$result = chmod($path, str_pad($permission, 4, '0', STR_PAD_LEFT));


have a little trick, change :

$result = chmod( $path, octdec( $permission ) );

to :

$result = chmod( $path, ( ( "0" . octdec( $permission ) ) * 1 ) );

hope this help.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号