1

I've php script that should create two new folders in "albums" and "pl". in "albums" apache can create a new folder but not in "pl", even though they have the same permissions. I can't understand what I'm doing wrong.

ls -Z /music
drwxrwxrwx. apache root system_u:object_r:unlabeled_t:s0 albums
drwxrwxrwx. apache root system_u:object_r:unlabeled_t:s0 pl

php

function createdir($dir) {
        if (!is_dir($dir)) {
            mkdir($dir);
                if (is_dir($dir)) {
                        echo "PASS<br />";
                }else{
                        echo "FAILED<br />";
                }

        }else{
                echo "The dir is there";
        }

}

createdir("/music/pl/07012013/");
createdir("/music/albums/06012013333333331111/");

su apache

su - apache -s /bin/bash
-bash-4.1$ mkdir /music/pl/06012013
mkdir: cannot create directory `/music/pl/06012013': Permission denied
3
  • what's in your script? how do you mkdir?
    – CSᵠ
    Jan 5, 2013 at 23:31
  • 1
    how about trying to run as apache and try to make a directory (via commandline). 'su apache' That way you can get better error response and diagnose the permissions issue you may have.
    – u8sand
    Jan 5, 2013 at 23:43
  • The user running the apache service probably doesn't have the right permissions. Jun 25, 2018 at 4:43

2 Answers 2

1

Check out php's mkdir.

You'll need to set recursive to true, so try something like mkdir($dir,0700,true)

1
  • didn't work ...
    – teslasimus
    Jan 6, 2013 at 11:07
-1

As this seems to be a common question with many answers that do not cover everything that I encountered; I will add my experience with this issue and the solution that did actually work.

I noticed that you are not setting the recursive flag, that you are not using the permissions flag and that you do not appear to be using the absolute path to the server.

As per the absolute path, it is relevant to ensuring that the apache user, www-data, actually does have permissions to modify everything in the immediate parent folder.

Have you tried the command with the permissions flag or experimented with the current umask and octal representation (according to PHP.net)? Presently the PHP documentation on mkdir indicates that if no permissions parameter is specified, Linux systems will default to "777" which is the least restrictive (and for security reasons not advised) and that the documentation also says that the permissions should probably be specified in octal, so a leading zero should be added to the permissions. There are also references to the current umask that you may want to check as well because if your current mask is non-zero the mask value is subtracted from the actual permissions set, if the current umask is 022 and you specify 777, the permissions you get are 755.

In my case, the PHP.net suggestions did not correct the problem and when I looked into how this problem was manifesting it turned out that the www-data user was lacking some of the needed permissions to access, modify or read the created folders and files. I tried setting the permissions flag as '6640' and it actually worked; moreover it caused all of the sub directories and files to inherit the '640' permissions (consider 640 and adding your account to the www-data group).

In short there are two options to try, one from the PHP documentation that did not work for me but may work for some:

$old = umask(0);
mkdir("/path/some_dir/", 0755, true);
umask($old);

The one that did work for me though with a somewhat peculiar behavior:

$old = umask(0000);
mkdir("/var/www/html/somewebsite/images/somefolder/somefolder/", 6640, true);
umask($old);

Note that I did not actually grant execute permissions in my script. I have noticed that there are a number of other variations of this question on other sites that have found some of the PHP.net information useful in their case, but I have included the answer that ended up working in my case. I hope this information will help some people at least.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .