Search code examples
phpincludeopen-basedirsafe-mode

PHP is_readable trouble with open_basedir


I've recently gotten an error when I deployed an application. It used "is_readable" on a path within the include path, but one that was restricted by "open_basedir". This gave me a fatal error. Is there another function that I could use to see if a file is includable, before actually including it?


Edit: this works, but how do I detect if the error was because the include failed or because of some error inside the included file?

try {
 include 'somefile.php';
 $included = true; 
} catch (Exception $e) {
 // Code to run if it didn't work out
 $included = false;
}

Solution

  • You could 'try' this ;)

    <?php
    
    function exceptions_error_handler($severity, $message, $filename, $lineno) {
        throw new ErrorException($message, 0, $severity, $filename, $lineno);
    }
    set_error_handler('exceptions_error_handler');
    try {
        include 'somefile.php';
        $included = true;
    } catch (Exception $e) {
        // Code to run if it didn't work out
        $included = false;
    }
    echo 'File has ' . ($included ? '' : 'not ') . 'been included.';
    ?>
    

    If it doesn't work, $included will be set to true, and then to false in the catch. If it did work, $included remains true.