mkdir() and why the default mode does not make sense

With the function mkdir() one can create direcotories in php. According to the manual the default permission for the mkdir() function is 0777 (NB: the zero in front means its in octal!) which means read/write and execute permissions for the owner, group and others. So the directory will be wide open. Wrong!

Most of the time – at least in my experience – the umask used on Unix/Linux systems is set to 0022. This means that the mode of the directory will be changed using this umask. This change can be calculated – as far as I understood – using this rule of thumb to calculate the permission of the directory created:

permissions mode - umask = permission of directory created

Or

0777 - 0022 = 0755

So in practice most of the time the directory will be created with the permissions set to 0755. This is kind of confusing when using mkdir() as I would understand that when I set the mode for the creation of a directory would be taking into account the current set umask.

My solution is to create a directory using mkdir() and after the succesful creation I use chmod($created_directory, 0777) to set the permission to whatever I want them to be.

Leave a Reply