Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change '0777' string to 0777 octal LITERALLY

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

like image 390
Xelakz Avatar asked Feb 23 '15 10:02

Xelakz


2 Answers

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)
like image 197
sectus Avatar answered Sep 29 '22 09:09

sectus


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));
like image 44
Xelakz Avatar answered Sep 29 '22 08:09

Xelakz