Page 1 of 1

Test directory permissions

Posted: Sat Jun 23, 2007 11:48 am
by Ambush Commander
It seems that directory permissions seems to be the bane of any application that wants to write to the filesystem. I have written a short little function that not only checks if a directory is writable by the script, but will give friendly error messages on how to fix file permissions if we can't write.

Comments?

Code: Select all

/**
     * Tests permissions on a directory and throws out friendly
     * error messages and attempts to chmod it itself if possible
     */
    function _testPermissions($dir) {
        // early abort, if it is writable, everything is hunky-dory
        if (is_writable($dir)) return true;
        if (!is_dir($dir)) {
            // generally, you'll want to handle this beforehand
            // so a more specific error message can be given
            trigger_error('Directory '.$dir.' does not exist',
                E_USER_ERROR);
            return false;
        }
        if (function_exists('posix_getuid')) {
            // POSIX system, we can give more specific advice
            if (fileowner($dir) === posix_getuid()) {
                // we can chmod it ourselves
                chmod($dir, 0755);
                return true;
            } elseif (filegroup($dir) === posix_getgid()) {
                $chmod = '775';
            } else {
                // PHP's probably running as nobody, so we'll
                // need to give global permissions
                $chmod = '777';
            }
            trigger_error('Directory '.$dir.' not writable, '.
                'please chmod to ' . $chmod,
                E_USER_ERROR);
        } else {
            // generic error message
            trigger_error('Directory '.$dir.' not writable, '.
                'please alter file permissions',
                E_USER_ERROR);
        }
        return false;
    }

Posted: Sat Jun 23, 2007 12:03 pm
by superdezign
I like it. It could be nice for us to use here at the boards when we suspect that users have a folder permissions error and they can't tell. I didn't even know we had an "is_writable()" function. :D

Posted: Sat Jun 23, 2007 1:37 pm
by Chris Corbyn
Love it! Concise and you've thought of everything I can think of (and more) :)

Posted: Sat Jun 23, 2007 1:51 pm
by alex.barylski
I could use that at work... :)

You should throw it into a little check.php script which you can invoke inside a directory. :)

Posted: Sat Jun 23, 2007 2:11 pm
by John Cartwright
What about moving it into an object, therefore you can return booleon on _testpermission without throwing errors, and optionally retrieving the errors with another method?

Posted: Sat Jun 23, 2007 2:41 pm
by Chris Corbyn
Jcart wrote:What about moving it into an object, therefore you can return booleon on _testpermission without throwing errors, and optionally retrieving the errors with another method?
I imagine this was written for code which requires a particular directory to be writable and where end users are constantly sending stupid bug reports asking why they're getting errors.... Effectively it's been written to improve the quality of an error PHP would already spit out :)

(Am I right Ambush?)

Posted: Sat Jun 23, 2007 2:44 pm
by John Cartwright
Never hurts to promote reusability. :wink:

Posted: Sat Jun 23, 2007 2:51 pm
by Ambush Commander
d11wtq's right. I got multiple bug reports over how file permissions had all blown up and I was compelled to write several paragraphs of documentation on how to chmod files properly, and figured that a better error would be helpful too. Making a fully-fledged object seems like overkill to me, especially since I don't think this code will be used anywhere else.

I hope that the code actually works. I don't have any real way of testing it because I don't have a spare Unix system (with root privileges) lying around.