如何检查包含路径下是否存在文件?
问题描述:
您可以使用 get_include_path()
我想知道在不发出 PHP 错误的情况下检查是否可以包含文件的轻量级方法是什么.我正在使用 Yii 框架,我想在不发出 PHP 错误的情况下进行导入,但我失败了.
I am wondering what is the lightweight way to check if the file can be included without issuing a PHP error. I am using Yii framework and I want to an import without issuing PHP error, but I fail.
答
在 PHP 5.3.2 之前,您可以拆分路径并在循环中检查每个路径:
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