我在其他文件中包含了一个php文件,在其他文件中包含了其他文件,所有文件都在不同的目录中

时间:2013-12-29 16:39:24

标签: php filepath

我有(/navigation/navigation.php)(/menu/whtuto.php)(/submenu/wh/1.php)

navigation.php已包含在whtuto.php (../navigation/navigation.php)中 和 whtuto.php包含在1.php (../../menu/whtuto.php)

但是当我加载1.php时它会出错!

 Warning: include(../navigation/navigation.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\hackingtuto\menupages\whtuto.php on line 3

Call Stack
#   Time    Memory  Function    Location
1   0.0011  364600  {main}( )   ..\11.php:0
2   0.0021  366824  include( 'C:\wamp\www\hackingtuto\menupages\whtuto.php' )   ..\11.php:3


Warning: include() [function.include]: Failed opening '../navigation/navigation.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\hackingtuto\menupages\whtuto.php on line 3

Call Stack
#   Time    Memory  Function    Location
1   0.0011  364600  {main}( )   ..\11.php:0
2   0.0021  366824  include( 'C:\wamp\www\hackingtuto\menupages\whtuto.php' )

3 个答案:

答案 0 :(得分:1)

您使用过嵌套包含。它将找到执行第一个php文件的相对路径。

要避免此问题,请使用

include (realpath(dirname(__FILE__)."/folder_name/myfile.php"));

答案 1 :(得分:0)

包含相对于当前工作目录,即第一个脚本的起始目录。您可以使用PHP魔术变量__DIR__获取当前包含文件的目录,这样您就可以相对于此包含 - 例如在whtuto.php中你应该使用include(__DIR__."/../navigation/navigation.php")等等。只需将__DIR__添加到您的包含路径中。您还需要在开头添加额外的斜杠,因为__DIR__没有斜杠。

答案 2 :(得分:0)

另一种选择是:

$root = $_SERVER['DOCUMENT_ROOT']; // Have this in every file you call your includes
include($root."/path/to/file.php");