Change '0777' string to 0777 octal LITERAL

2019-04-30 18:50发布

问题:

My code is like

$perm = "0777"; //this is fetch from the database
chmod("myFolder/", $perm);

but the value of $perm is not in octal, how can I change the data type of the variable to octal? even an alternative method will do

回答1:

As it was mentioned, there is no octal number type. And chmod function receive the second param as integer number. Implicit conversion of $perm does not assume that number is octal. So, you need convert your "octal string" to integer by using appropriate function.

Just use octdec function

$perm = "0777"; //this is fetch from the database
chmod("myFolder/", octdec($perm));

Or intval

chmod("myFolder/", intval($perm, 8));

P.S.

var_dump('0644' == 0644);             // bool(false)
var_dump(intval('0644') == 0644);     // bool(false)
var_dump(decoct('0644') == 0644);     // bool(false)

var_dump(octdec('0644') == 0644);     // bool(true)
var_dump(intval('0644', 8) == 0644);  // bool(true)


回答2:

Nothing seemed to work for me so i just created a very stupid solution and I'm just gonna post it here.

function permtooct($permissions) {      

    $mode = 0; 
    if ($permissions[0] == '1') $mode += 01000; 
    if ($permissions[0] == '2') $mode += 02000; 
    if ($permissions[0] == '3') $mode += 03000; 
    if ($permissions[0] == '4') $mode += 04000; 
    if ($permissions[0] == '5') $mode += 05000; 
    if ($permissions[0] == '6') $mode += 06000; 
    if ($permissions[0] == '7') $mode += 07000; 

    if ($permissions[1] == '1') $mode += 0100; 
    if ($permissions[1] == '2') $mode += 0200; 
    if ($permissions[1] == '3') $mode += 0300; 
    if ($permissions[1] == '4') $mode += 0400; 
    if ($permissions[1] == '5') $mode += 0500; 
    if ($permissions[1] == '6') $mode += 0600; 
    if ($permissions[1] == '7') $mode += 0700; 

    if ($permissions[2] == '1') $mode += 010; 
    if ($permissions[2] == '2') $mode += 020; 
    if ($permissions[2] == '3') $mode += 030; 
    if ($permissions[2] == '4') $mode += 040; 
    if ($permissions[2] == '5') $mode += 050; 
    if ($permissions[2] == '6') $mode += 060; 
    if ($permissions[2] == '7') $mode += 070; 

    if ($permissions[3] == '1') $mode += 01; 
    if ($permissions[3] == '2') $mode += 02; 
    if ($permissions[3] == '3') $mode += 03; 
    if ($permissions[3] == '4') $mode += 04; 
    if ($permissions[3] == '5') $mode += 05; 
    if ($permissions[3] == '6') $mode += 06; 
    if ($permissions[3] == '7') $mode += 07; 

    return($mode);
}
$a = "0777";
chmod("myFolder/", permtooct($a));


回答3:

Octal is not a type. Integer is the type.

You want to convert a string to an int, and because of that the string starts with a zero, it will be an octal integer.

Use intval for this:

chmod('myFolder/', intval($perm));


回答4:

I needed to feed the mode component of chmod() as a variable inside a function. I spent sufficient time trying (and failing) to convert the string into an octal while maintaining the 0777 value. In the end, I decided to abandon chmod()/ftp_chmod() and opted for ftp_site() which doesn't choke on string-type mode codes.

if(!ftp_site($connection,"chmod {$mode} {$path}/{$file}")){
    // handle the error
}

This worked for me. If this is somehow flawed or not best practice, please explain.

Edit: Some readers may reject this ftp_ solution as cheating because it is not a filesystem function solution. It was a non-issue for me because I was writing a function in a ftp class.