Issue
mkdir function has default value for parameter mode as 0777
.
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
Every example of mkdir I have seen, has mode parameter starting with a 0. Doesn't the leading octal digit signify the type of file?
From this page, 0 means a regular file while 3 means a directory.
So, shouldn't the default value for mode parameter in mkdir be 3777
as opposed to 0777
.
What is the difference between the two modes with respect to mkdir. If I am creating a regular folder, which mode value should I use?
Solution
You can get some pretty confusing information depending on whom you ask. The modes you specify with chmod
, mkdir
, touch
and other tools that create and modify file permissions tend to deal only with the lower-order permission bits, and sometimes the sticky and set[ug]id bits. The higher-order parts of the mode, which ls -l
and stat
output, but are less often seen, include the file type.
The rightmost nine bits are all entirely occupied by permissions, and the next several bits are related to sticky, setuid and segid bits. Note:
bin(0777) => '0b111111111' # 9 bits, all read, write, exec
bin(01777) => '0b1111111111' # 10 bits, sticky + 0777
bin(02777) => '0b10111111111' # 11 bits, setgid + 0777
bin(04777) => '0b100111111111' # 12 bits, setuid + 0777
So that article is a little confusing considering there's no space for the file type to fit within the first 12 bits.
You seem to be clear on this in the question, but for posterity, the leading 0
is what identifies the value as octal. So even if you wanted to specify a file type, you'd certainly have to start with something like 03…
.
In any case, mkdir
is going to make a directory or nothing. It won't make a file, so it's not concerned with the file type bit.
Answered By - kojiro Answer Checked By - Candace Johnson (WPSolving Volunteer)