我将以下PHP脚本放在我的网络托管服务器的根目录中,它运行正常。
<?php
include(dirname(__FILE__).'/config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');
当我将脚本移动到我的Web根目录下的新文件夹(即public_html/myfolder
)时,脚本会出现以下错误:
Warning: include(/home/name/public_html/folder/config/config.inc.php) [function.include]: failed to open stream: No such file or directory in /home/name/public_html/folder/test.php on line 2
Warning: include() [function.include]: Failed opening '/home/name/public_html/folder/config/config.inc.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/name/public_html/folder/test.php on line 2
Warning: require_once(/home/name/public_html/folder/init.php) [function.require-once]: failed to open stream: No such file or directory in /home/name/public_html/folder/test.php on line 3
Fatal error: require_once() [function.require]: Failed opening required '/home/name/public_html/folder/init.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/name/public_html/folder/test.php on line 3
我应该按如下方式更改路径:
<?php
include(dirname(__FILE__).'/../config/config.inc.php');
require_once(dirname(__FILE__).'/../init.php');
我说错了吗?
答案 0 :(得分:0)
嗯,你说你只将你的脚本(包含include
行的脚本)移动到“public_html / folder”。这样,dirname(__FILE__)
包含完整路径,包括“folder”目录。所以,现在,你需要在目录树中向上一步,才能到达“config”目录;解释如下:
之前:
/home/name/public_html/config/config.inc.php
/home/name/public_html/init.php
所以你的文件
/home/name/public_html/test.php
使用dirname(__FILE)
的路径管理访问文件。但现在,test.php已移至目录“folder”:
/home/name/public_html/folder/test.php
因此,dirname(__FILE__)
应该返回“folder”目录的综合路径。但是您需要包含的文件位于“旧”目录中,因此您需要将../
添加到include的路径中,以便能够使用这些文件:)