I've the next simple script to check the mkdir() function in PHP:

  $id = rand();

  $targetPath = $_SERVER['DOCUMENT_ROOT'] . '/sample_folder/' . $id .'/';
  mkdir(str_replace('//','/',$targetPath), 0755, true);

Ideally, this could produce a random folder every time the script operates under my web directory/sample_folder. Sample_folder has 755 permissions.

The problem I face is, I keep encountering PHP: mkdir() Permission refused issues. My sample_folder permissions are presently set to chmod 755.

EVERYTHING I've read states to not chmod to 777 so do not suggest it.

For test reasons, chmod 777 the 'sample_folder' directory addresses the problem however this poses security issues. Can there be another thing I'm missing how to make this happen?

Of note: my PHP customers around the product is "apache"

I'm running PHP 5.3.* and CentOS 5.5 on the Media Temple devoted virtual server for reference. I've also looked through virtually every chmod question on SO and can't appear to locate a solution that suits my problem (except for 777 suggestions).


Running ls -la on my small server returns:

drwxr-xr-x 2 ftphiddenname psacln 4096 Jan 26 11:24 sample_folder

final update

The solutions provided were very useful. For anyone searching for more information, I discovered this understanding base article and even though it is for auction on Media Temple, I blieve the concepts affect any most similar designs:

(dv):Resolve Apache permission errors

The reason behind this is actually the script needs write permissions in sample_folder.

I'm not sure your actual setup, but I am speculating your script is either running under world permissions or group permission that is 5 (read 4 + execute 1) as your current permissions are 755 (7 for owner, 5 for group and 5 for world). To create sites into that folder, your script will require write access. You are able to set this up more safely than 777 if you can get chown sites. My advice is always to produce a group known as 'webgroup' or similar and put your webserver user into that group. Then, provide the group write permissions (770) could be appropriate after you have that setup. Just in case you are just a little hazy how the permissions work, the ultimate setup could be:

sample_folder: possessed by root, group webgroup, 770 permissions add whatever user apache (or any other webserver) is running regarding webgroup


With increased particulars obtainable in the first publish, which means you could be adding the consumer 'apache' to webgroup. If you discover that as well difficult a setup or you don't have full permissions around the server to create this up then using chown as recommended elsewhere to allow apache own your directory works too.

For instance: chown apache sample_folder

This makes apache who owns the folder, passing on use of write permissions (presuming it's still 755)

Perhaps you have checked the filesystem permissions for that folder to which you are attempting to produce your directory?

You need to cd in to the directory and ls -la to determine the present possession and filesystem permissions:

paulbain@test ~/test $ ls -la
total 8
drwxr-xr-x  2 paulbain apache 4096 Jan 26 17:38 .

During my example above, it's owner Read Write Execute, Group Read and Execute and Everybody Read and Execute.

If the was the situation and you've got your Apache running within user within the group apache, PHP wouldn't have the ability to produce the directory.