1

I have the following PHP script placed in the root directory of my web hosting server, and it works fine.

<?php
include(dirname(__FILE__).'/config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');

When I moved the script to a new folder under my web root, i.e. public_html/myfolder, the script gives the following error:

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

Should I change the path as follows:

<?php
include(dirname(__FILE__).'/../config/config.inc.php');
require_once(dirname(__FILE__).'/../init.php');

Am I correct?

4

1 回答 1

0

嗯,您说您只是将脚本(包含include行的脚本)移动到“public_html/folder”。这样,dirname(__FILE__)包含完整路径,包括“文件夹”目录。所以,现在,你需要在目录树上上一层,才能到达“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 已经被移动到目录“文件夹”:

/home/name/public_html/folder/test.php

因此,dirname(__FILE__)应该返回“文件夹”目录的综合路径。但是您需要包含的文件位于“旧”目录中,因此您需要将其添加../到包含的路径中,以便能够访问这些文件:)

于 2013-05-28T12:30:18.213 回答