Search code examples
phpmkdir

Mkdir throws a warning, when it should be a bool?


so the popular mkdir() function in php throws me a warning in the following code:

public function check_dir($dir, $create_dir = false) {
    if (is_dir ( $dir )) {
        return true;
    } elseif ($create_dir == true) {
        return mkdir ( $dir );
    }

    return false;
}

Its simple to know whats going on. So I won't explain. But on the mkdir() line I get:

Warning: mkdir(): Permission denied in /var/www/wordpress/wp-content/themes/Aisis-Framework/AisisCore/FileHandling/File.php on line 70

So while its a warning and nothing more, you should never turn of warnings in live, or any kind of error for that fact. So how do I deal with this when its clearly states it returns true or false and not a warning or fatal?


Solution

  • You can establish a custom error handler, which is a good practice in general: http://www.php.net/set-error-handler

    You can use this to handle any PHP errors in any way you find appropriate. Short of that, you would have to either turn off error/warning logging (as you say, not a good practice), or use @ suppression (which should be avoided in general, but might be suitable in this case.)

    Personally I would agree that a function that returns true/false to indicate failure already doesn't need to issue a warning if it fails. But, that's PHP for you.