这很奇怪,有人总结出结论了吗?
有时它也会检查包含文件的目录。
但有时不会。
D:\test\1.php
<?php
include('sub\2.php');
D:\test\2.php
<?php
include('3.php');
3.php
与2.php
在同一目录下。
上述方法可以工作,但是为什么呢?当前目录应该是D:\test
,但它仍然可以找到位于D:\test\sub
中的3.php。
更多故事(最终版)
大约一年前,我遇到了这个问题,然后我用以下硬编码解决了它:
Common.php:
if (file_exists("../../../Common/PHP/Config.inc"))
include('../../../Common/PHP/Config.inc');
if (file_exists("../../Common/PHP/Config.inc"))
include('../../Common/PHP/Config.inc');
if (file_exists("../Common/PHP/Config.inc"))
include('../Common/PHP/Config.inc');
if (file_exists("Common/PHP/Config.inc"))
include('Common/PHP/Config.inc');
Config.inc
与Common.php
在同一目录中。
include(dirname(__FILE__).'/Config.inc');
- 这将始终起作用,无论include_path
和包含“Common.php”的文件是哪个。如果没有机会在include_path
(其中通常包括当前目录)中找到“Config.inc”,那么您可以简单地调用include 'Config.inc';
,尽管这可能效率较低,因为首先搜索include_path
(失败)。 - MrWhiteinclude('sub\2.php');
将永远不起作用。) - MrWhite