Cannot get correct permissions for mkdir () in PHP

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 will create a random folder each time the script is run in my web directory / sample_folder. Sample_folder has 755 permissions.

The problem I am facing is that I continue to work in PHP: mkdir () Permission denied issues. My sample_folder permissions are currently set to chmod 755.

EVERYTHING I have a reading state not from chmod to 777, so please do not suggest this.

For testing purposes, chmod 777 refers to the sample_folder directory, but again this creates security issues. Is there anything else that I am missing on how to make this work?

Note: my PHP users on the system are "apache";

I am using PHP 5.3. * and CentOS 5.5 on the dedicated Media Temple virtual server for reference. I also looked through almost every chmod question about SO and cannot find a solution that matches my problem (with the exception of 777 suggestions).

change

Running ls -la on my server returns:

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

final update

The answers received were very helpful. For anyone looking for additional information, I came across this knowledge base article and, as long as it is listed in Media Temple, I blieve the principles apply to any of the most similar configurations:

(dv): resolve Apache permission errors

+4
source share
2 answers

The reason for this is because the script needs write permissions in sample_folder.

I do not know your actual setup, but I assume that your script works either under the rights of the world, or with the rights of a group that is 5 (read 4 + execute 1), since your current permissions are 755 (7 for the owner, 5 for the group and 5 for the world). To write directories to this folder, your script will need write access. You can install it safer than 777 if you have access to chown katanas. My advice would be to create a group called "webgroup" or similar, and put the web server user in this group. Then give permission to write to the group (770), if you have such a setting. In case you are a bit foggy about how permissions work, the final setup will be as follows:

sample_folder: belongs to root group, groupgroup, 770 add any custom apache (or other web server) for the web group

EDIT:

With more detailed information available in the original post, this means that you are adding a custom "apache" to the web group. If you find that it is too difficult to configure or you do not have full permissions on the server to install it, then use chown, as suggested elsewhere, to let apache's own directory work as well.

For example: chown apache sample_folder

This will make apache the owner of the folder, giving it access to write permissions (provided that it is still 755)

+2
source

Have you checked the file system permissions for the folder in which you are trying to create a directory?

You should cd into the directory and ls -la see the current ownership and file system rights:

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

In my example above, its owner reads Write Execute, Group Read and Execute, and Everyone Read and Execute.

If so, and your Apache is running as a user in the apache group, PHP will not be able to create the directory.

+1
source

Source: https://habr.com/ru/post/1393220/


All Articles