Change '0777' string to 0777 octal LITERAL

2019-04-30 19:06发布

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

4条回答
Deceive 欺骗
2楼-- · 2019-04-30 19:38

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));
查看更多
Lonely孤独者°
3楼-- · 2019-04-30 19:40

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.

查看更多
狗以群分
4楼-- · 2019-04-30 19:43

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));
查看更多
不美不萌又怎样
5楼-- · 2019-04-30 19:47

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)
查看更多
登录 后发表回答