Change the string "0777" 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 octal, how can I change the data type of a variable to octal? even an alternative method will do

+5
source share
4 answers

As already mentioned, there is no octal type. And the chmod function gets the second parameter as an integer. The implicit conversion of $perm does not imply that the number is octal. Thus, you need to convert the "octal string" to an integer using the appropriate function.

Just use the octdec function

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

Or intval

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

PS

 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) 
+4
source

Octal is not a type. Integer is a type.

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

Use intval to do this:

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

Nothing seemed to work for me, so I just created a very stupid solution and I will just 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)); 
0
source

I needed to pass the chmod() mode component as a variable inside the function. I spent enough time trying (and could not) convert the string to octal, keeping the value 0777. In the end, I decided to abandon chmod() / ftp_chmod() and chose ftp_site() , which does not suffocate from string type mode codes.

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

It worked for me. If this is somehow spoiled or not recommended, explain.

Edit: Some readers may dismiss this ftp_ solution as a hoax because it is not a solution to the file system function. This was not a problem for me, because I wrote a function in the ftp class.

0
source

Source: https://habr.com/ru/post/1213976/


All Articles