Change '0777' string to 0777 octal LITERALLY

chmod, octal, permissions, php

Solution

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)

Problem

My code is like ``` $perm = "0777"; //this is fetched 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

Original source