Test directory permissions

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Test directory permissions

Post 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;
    }
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Love it! Concise and you've thought of everything I can think of (and more) :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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. :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Never hurts to promote reusability. :wink:
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply