我有(/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:wampwwwhackingtutomenupageswhtuto.php on line 3
Call Stack
# Time Memory Function Location
1 0.0011 364600 {main}( ) ..11.php:0
2 0.0021 366824 include( 'C:wampwwwhackingtutomenupageswhtuto.php' ) ..11.php:3
Warning: include() [function.include]: Failed opening '../navigation/navigation.php' for inclusion (include_path='.;C:phppear') in C:wampwwwhackingtutomenupageswhtuto.php on line 3
Call Stack
# Time Memory Function Location
1 0.0011 364600 {main}( ) ..11.php:0
2 0.0021 366824 include( 'C:wampwwwhackingtutomenupageswhtuto.php' )
您已经使用了嵌套包含。它将从执行的第一个php文件中找到相对路径。
为了避免这个问题,使用
include (realpath(dirname(__FILE__)."/folder_name/myfile.php"));
Includes是相对于当前工作目录的,当前工作目录是第一个脚本启动的目录。您可以使用PHP魔术变量__DIR__
获取当前include文件的目录,因此您可以使include相对于该目录-例如,在whtuto.PHP中,您应该使用include(__DIR__."/../navigation/navigation.php")
等。只需在include路径前加上__DIR__
即可。您还需要在开头添加一个额外的斜杠,因为__DIR__
没有尾部斜杠。
另一个选项是:
$root = $_SERVER['DOCUMENT_ROOT']; // Have this in every file you call your includes
include($root."/path/to/file.php");