If the question Whats the difference between 0777 vs 777 file modes?
runs through your mind, you’re not alone. Lets find out…
A bit of history,
I needed to create a directory (using Php). So as per the syntax of mkdir()
, I used
mkdir('/path/to/create/dir', 777);
where the 2nd passed parameter is the file mode.
Normally, this should have worked fine and uploading any file to this folder should have worked too, given that I gave read/write/execute access as seen below to everyone.
Astonishingly, it did not.
I was unable to write to the directory. And I couldn’t understand why. I checked the file permissions using ls only to realize the folder permissions hadn’t been set to 777.
Was there a problem with mkdir() ? The syntax seemed fine and there were no errors thrown. So then what was happening?
The Difference
Turns out that the second parameter you pass to mkdir() is interpreted as decimal if it isn’t preceded by a 0 whereas mkdir() expects the numeric to be an Octal. Hence prefixing the file mode with a 0 is utterly important.
Also, Why 777 doesn’t work is because the bit-pattern of 777 is quite different from 0777
0777 vs 777
0777 (octal) == binary 0b 111 111 111 == permissions rwxrwxrwx (== decimal 511) 777 (decimal) == binary 0b 1 100 001 001 == permissions sr----x--x (== octal 1411)
Note
It is worthy to note that if you’re using chmod (the command line program), then there is no difference between 777 and 0777.
This is because chmod
interprets all numeric arguments as octal.
However, while using Php, Python, Ruby or a C program, your file mode should be prefixed with a 0 so as to be interpreted correctly.
One comment