How to check if a file exists under include path?

前端 未结 2 979
小鲜肉
小鲜肉 2021-02-19 16:45

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

2条回答
  •  一整个雨季
    2021-02-19 17:20

    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
    

提交回复
热议问题