我有(/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' )
答案 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");