I have the following simple script to test the mkdir() function in PHP:
<?php
$id = rand();
$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/sample_folder/' . $id .'/';
mkdir(str_replace('//','/',$targetPath), 0755, true);
?>
Ideally, this would create a random folder each time the script is run under my web directory/sample_folder. Sample_folder has 755 permissions.
The issue I face is, I keep running into PHP: mkdir() Permission denied issues. My sample_folder permissions are currently set to chmod 755.
EVERYTHING I have read states not to chmod to 777 so please don't suggest it.
For test purposes, chmod 777 the 'sample_folder' directory addresses the issue but again this poses security issues. Is there something else I am missing on how to make this work?
Of note: my PHP users on the system is "apache";
I am running PHP 5.3.* and CentOS 5.5 on a Media Temple dedicated virtual server for reference. I have also looked through nearly every chmod question on SO and cannot seem to find a solution that matches my issue (with the exception of 777 suggestions).
edit
Running ls -la on my server returns:
drwxr-xr-x 2 ftphiddenname psacln 4096 Jan 26 11:24 sample_folder
final update
The answers provided were very helpful. For anybody looking for additional information, I came across this knowledge base article and while it is listed on Media Temple, I blieve the principles apply to any most similar configurations:
chmoding, you need tochownto ensure that755is adequate for you to modify the directory in question. You would need7(6?) on the parent directory in order to do this - so if the parent directory is not owned by the user PHP is executing as, it won't let you do it with755. - DaveRandomls -lin the parent directory's parent you can see the permissions and ownership on the parent (if you follow that, I would forgive you if you didn't). Essentially, in your example, you shouldls -lthe document root, you will see the info you want. Really all you need to do (as a user with permission to do it) ischown apache <document root>/sample_folder- DaveRandomapacheuser (anything you create with PHP should do this), orchownthem toapachewhen you create them. - DaveRandom