我想检查“ad.php”中是否包含“dbas.php”。我写了代码 -
ad.php
<?php if(file_exists("dbas.php") && include("dbas.php")){
// some code will be here
}
else{echo"Database loading failed";}
?>
我成功地测试了 file_exists() 部分,但不知道 include() 是否能正常工作,因为我在 localhost 中尝试过,如果文件在目录中,那么它永远不会失败。所以我不知道如果流量很大,这段代码在服务器中会如何表现。所以请告诉我我的代码是否正确?
-谢谢。
已解决:非常感谢您的回答。
最佳答案
使用 php 的 require
method如果您想绝对确定该文件包含在内,则更合适。 file_exists
仅检查文件是否存在,而不检查文件是否真正可读。
require
将产生错误(您可以捕获
错误,请参阅 Cerbrus 的回答)。
编辑:
但是,如果您不希望脚本在包含失败时停止,请使用方法 is_readable
和 file_exists
,例如:
if( file_exists("dbas.php") && is_readable("dbas.php") && include("dbas.php")) {
/* do stuff */
}
关于php - 如何检查一个php文件是否被成功包含?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13739043/