开发者

How to check if a file exists under include path?

开发者 https://www.devze.com 2023-03-07 03:05 出处:网络
You get the current include path in PHP by using get_include_path() I am wondering what is the lightweight way to check if the file can be included without issuing a PHP error. I am usin开发者_JAVA技

You get the current include path in PHP by using get_include_path()

I am wondering what is the lightweight way to check if the file can be included without issuing a PHP error. I am usin开发者_JAVA技巧g Yii framework and I want to an import without issuing PHP error, but I fail.


As of PHP 5.3.2, you can use

  • stream_resolve_include_path — Resolve filename against the include path

which will

Returns a string containing the resolved absolute filename, or FALSE on failure.

Example from Manual:

 var_dump(stream_resolve_include_path("test.php"));

The above example will output something similar to:

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


Before PHP 5.3.2 you can split the path and check each path in a loop:

$find = 'file.php'; //The file to find
$paths = explode(PATH_SEPARATOR, get_include_path());
$found = false;
foreach($paths as $p) {
  $fullname = $p.DIRECTORY_SEPARATOR.$find;
  if(is_file($fullname)) {
    $found = $fullname;
    break;
  }
}
//$found now contains the file to be included, or false if not found
0

精彩评论

暂无评论...
验证码 换一张
取 消