Question

We are using a linux dedicated server in our company.

Whenever we create a folder with php script, apache is owning the folder and each time I have to connect to server via root in order to change the owner to the user and the permissions

But I don't know which setting I should change in order to fix this.

I tried to switch the safe mode on/off but it didn't work.

I'd be glad if someone could help me

Thanks in advance

Was it helpful?

Solution

This is a portability issue and depends on your server configuration (i.e. mod_php or FCGI with SuExec).

You can try using the chown function right after you created the file, but that will only work if you know who the actual owner should be (which is not always the case). To overcome this issue, I wrote a simple method that tries to find the best possible file mode that always assures reading and writing (as well as execution for directories) without giving excessive permissions:

function xChmod($path, $chmod = null) // adapted from phunction
{
    if (file_exists($path) === true)
    {
        if (is_null($chmod) === true)
        {
            $chmod = (is_dir($path) === true) ? 777 : 666;

            if (extension_loaded('posix') === true)
            {
                $user = posix_getpwuid(posix_getuid());

                if (array_key_exists('name', $user) === true)
                {
                    $chmod -= (in_array($user['name'], explode('|', 'apache|httpd|nobody|system|webdaemon|www|www-data')) === true) ? 0 : 22;
                }

            }
        }

        return chmod($path, octdec(intval($chmod)));
    }

    return false;
}

Basically, if you omit the second $chmod argument, it will use 0777 for folders and 0666 for files by default, however, if PHP is being run under a non-standard user, it will decrement the mode by 22, yielding 0755 for folders and 0644 for files. Bare in mind that this will only work for files that already exist, so if you're creating a directory you'll need to have additional precautions.

OTHER TIPS

Have you tried using the chown PHP command and chmod PHP command?

There are 2 ways to do this:

chown() where you can change the file permissions via executing that function.

or if you are able to move files around using an FTP, you can drag the file outside of the document root (public_html/), and right click -> change file permissions on the file.

CHMODs are what you want to be looking at. 777 will give anyone and everything access to your file, which you can use as a test to make sure you have file permissions. I don't recommend keeping the permissions on 777 at all though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top