Current Location: Home> Latest Articles> get_include_path() with is_readable() Check whether the file is readable

get_include_path() with is_readable() Check whether the file is readable

gitbox 2025-05-26

In PHP, get_include_path() and is_readable() are two very practical functions, which are particularly important when dealing with file inclusion and access permission judgments. This article will explain how to use these two functions in combination to determine whether a file exists in the path specified by include_path and has readable permissions.

1. What is get_include_path() ?

get_include_path() is a built-in function that returns the current include path (including_path). The include path is a set of directories used by PHP when looking for included files (such as include or require ). This function can be used to obtain the path to manually check whether the file exists in a directory in it.

 $includePath = get_include_path();
echo $includePath;

Its return value is usually a string composed of multiple paths, and the paths are connected by operating system-related path separators (for example, colon ":" under Unix/Linux and semicolon ";" under Windows).

2. What is is_readable() ?

is_readable() is used to determine whether a file exists and has readable permissions. It returns a Boolean value, true means the file exists and is readable, and false means the file is inaccessible or does not exist.

 if (is_readable('/path/to/file.php')) {
    echo "File readable";
} else {
    echo "File not readable";
}

3. Use these two functions in combination

Sometimes we don't know the exact location of the file, we only know that it may exist in a directory specified by include_path . At this point, we can use get_include_path() and is_readable() to determine whether the file exists and is readable in these directories.

Here is an example function to check if a file name is readable in the current include_path :

 function isFileReadableInIncludePath($filename) {
    $paths = explode(PATH_SEPARATOR, get_include_path());

    foreach ($paths as $path) {
        $fullPath = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $filename;
        if (is_readable($fullPath)) {
            return $fullPath; // Returns the full path to the readable file
        }
    }

    return false; // No readable files are found in all paths
}

Example of usage:

 $file = 'config.php';

$result = isFileReadableInIncludePath($file);

if ($result !== false) {
    echo "File readable,The path is:$result";
    include $result;
} else {
    echo "The file is in include_path Not readable or does not exist";
}

4. Practical application scenarios

Suppose you are developing a large application, and the configuration file may be located in one of multiple paths, for example:

  • /usr/local/lib/myapp/

  • /home/user/myapp/includes/

  • Current working directory

You can add these paths to PHP's include_path :

 set_include_path(
    get_include_path() . PATH_SEPARATOR .
    '/usr/local/lib/myapp/' . PATH_SEPARATOR .
    '/home/user/myapp/includes/'
);

Then use the above functions to safely include the configuration file without worrying about the file not exist or has no permissions:

 $configFile = isFileReadableInIncludePath('config.php');

if ($configFile) {
    include $configFile;
} else {
    die('Configuration files are not accessible');
}

5. Things to note

  1. is_readable() checks the readability at the operating system level, which is related to PHP's open_basedir restrictions. Be sure to make sure that the configuration does not prevent PHP from accessing related directories.

  2. When using the file name entered by the user, verification should always be carried out to prevent security vulnerabilities such as directory traversal.

  3. If you use an automatic loading mechanism (such as Composer) in your project, this method may no longer be required and needs to be used according to the actual needs of the project.

6. Conclusion

By using the combination of get_include_path() and is_readable() , it is possible to determine whether the file exists in the included path and is readable without clarifying the complete path of the file. This is especially practical in modular design or multipath deployment scenarios. Using these functions reasonably can help improve the robustness and maintainability of the code.