我有一个包含常规配置的配置文件(在 git 存储库中),以及一个覆盖配置属性的本地配置文件(在存储库中被忽略)。现在本地配置文件包含在配置文件的开头:
include_once 'local_config.php';
但我希望包含是有条件的:仅当文件 local_config.php 实际存在时才执行此操作。我可以做一个在此输入链接描述 https://stackoverflow.com/questions/9138444/using-php-include-during-conditional-statements没有问题,但首先我需要检查该文件是否存在。所以我尝试了 get_include_path() 但它返回一个路径列表,我必须解析这个列表并检查每个路径。
另一种选择是只调用 include_once() 并抑制警告,但它更混乱。有没有更简单的方法在 PHP 中执行真正的可选包含?
Use the file_exists()
预定义的 PHP 函数如下:
// Test if the file exists
if(file_exists('local_config.php')){
// Include the file
include('local_config.php');
}else{
// Otherwise include the global config
include('global_config.php');
}
文档在这里:http://php.net/manual/en/function.file-exists.php http://php.net/manual/en/function.file-exists.php
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)