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.
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.
/**
* 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;
}
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.
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?
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
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.