PHP is_readable problem with open_basedir

I recently received an error message while deploying an application. He used "is_readable" in the path inside the include path, but one that was limited to "open_basedir". This gave me a fatal error. Is there another function that I could use to see if a file is included before including it?


Edit: this works, but how can I determine if the error was an error because the failure was on 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;
}
+3
source share
3 answers

You can "try" it;)

<?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.';
?>

, $included true, false catch. , $includes .

+2

rescriction open_basedir ( ),

ini_get( 'open_basedir' );

, .

Edit:

open_basedir :

if ( strlen( ini_get( 'open_basedir' ) ) > 0 )
{
    $includeFile = 'yourInclude.php';
    $includePath = dirname( realpath( $includeFile ) );

    $baseDirs = explode( PATH_SEPARATOR, ini_get( 'open_basedir' ) );
    foreach ( $baseDirs as $dir )
    {
        if ( strstr( $includePath, $dir ) && is_readable( $includeFile ) )
        {
            include $includeFile;
        }
    }
}

, .

+1

You can try to use stat to achieve the same effect as is_readable, which, as I hear, is very difficult to set up the base dir.

0
source

Source: https://habr.com/ru/post/1718706/


All Articles