stream_resolve_include_path

(PHP 5 >= 5.3.2, PHP 7, PHP 8)

stream_resolve_include_path Resolve filename against the include path

说明

stream_resolve_include_path ( string $filename ) : string|false

Resolve filename against the include path according to the same rules as fopen()/include.

参数

filename

The filename to resolve.

返回值

Returns a string containing the resolved absolute filename, 或者在失败时返回 false.

范例

Example #1 stream_resolve_include_path() example

Basic usage example.

<?php
var_dump
(stream_resolve_include_path("test.php"));
?>

以上例程的输出类似于:

string(22) "/var/www/html/test.php"

User Contributed Notes

kawewong at gmail dot com 07-Feb-2021 07:42
In some case like this, you can't use `realpath()` or `file_exists()` without resolve its path.

Example:

file.php
subfolder/
..|- included.php
..|- subfolder/
.........|- another-included.php

file.php contents:
```
<?php
var_dump
(file_exists('subfolder/included.php'));// true
include 'subfolder/included.php';
?>
```

subfolder/included.php contents:
```
<?php
var_dump
(file_exists('subfolder/another-included.php'));// false but the file is really exists.
var_dump(file_exists(stream_resolve_include_path('subfolder/another-included.php')));// with `stream_resolve_include_path()` function, it returns true now.
include 'subfolder/another-included.php';// working fine, no errors.
?>
```

subfolder/subfolder/another-included.php contents:
```
<?php
echo 'Hello world';
?>
```
zelnaga at gmail dot com 17-Jun-2014 02:48
In case you're running a version of PHP that doesn't have this function...

if (!function_exists('stream_resolve_include_path')) {
    /**
     * Resolve filename against the include path.
     *
     * stream_resolve_include_path was introduced in PHP 5.3.2. This is kinda a PHP_Compat layer for those not using that version.
     *
     * @param Integer $length
     * @return String
     * @access public
     */
    function stream_resolve_include_path($filename)
    {
        $paths = PATH_SEPARATOR == ':' ?
            preg_split('#(?<!phar):#', get_include_path()) :
            explode(PATH_SEPARATOR, get_include_path());
        foreach ($paths as $prefix) {
            $ds = substr($prefix, -1) == DIRECTORY_SEPARATOR ? '' : DIRECTORY_SEPARATOR;
            $file = $prefix . $ds . $filename;

            if (file_exists($file)) {
                return $file;
            }
        }

        return false;
    }
}
tambet dot matiisen at gmail dot com 28-Mar-2013 12:26
stream_resolve_include_path() seems to cache it's output. After I renamed a file, I had to restart Apache for stream_resolve_include_path() to not return non-existing file name. This was on Windows.
kontakt at victorjonsson dot se 25-Apr-2012 08:13
This seems to be a great alternative to file_exists.

if( file_exists(__DIR__.'/som-file.php') )

Goes way slower than:

if( stream_resolve_inlcude_path(__DIR__.'/som-file.php') !== false)
sebastian dot krebs at kingcrunch dot de 16-Feb-2011 03:03
It really behaves like `include` and will only resolve the filename against the include-path, if the path is relative. It makes not much sense to resolve already absolute pathnames anyway.