Creating a directory in php

If your website is shared on *nix server, a directory created through mkdir() will not be assigned to you, but to the user that your host’s server or php process is running under, usually ”apache’ or ‘httpd’.

In practice, this means that you can create directories, even add files to them, but you can’t delete the directory or its contents nor change permissions to a directory.

It is therefore advised to create directories through PHP’s FTP API using PHP function. This is a function I wrote:

Hope this comes in handy for someone.

You might notice that when you create a new directory using this code: mkdir($dir, 0777); The created folder actually has permissions of 0755, instead of 0777. Why is this? Because of umask() in php engine.

The default value of umask, at least on my setup, is 18. Which is 22 octal, or
0022. This means that when you use mkdir() to CHMOD the created folder to 0777,
PHP takes 0777 and substracts the current value of umask, in our case 0022, so the result is 0755 – which is not what you expected.

The “fix” for this is simple, include this line: $old_umask_value = umask(0);

Right before creating a folder with mkdir() to have the actual value you put be used as the CHMOD. If you would like to return umask to its original value when you’re done, use this php function: umask($old_umask_value);