如何让require_once从上面的目录中获取文件?

时间:2013-09-23 19:06:45

标签: php apache pear require-once

我正在尝试在新服务器上设置一个我不是原始所有者的站点 - 在Apache2中,文档根目录已设置为/ var / www / html,这是index.php的位置我希望成为默认的“主页”。

www中的其他目录中有许多文件。例如,/ common / cache / Lite.php也可以在/ var / www中找到。

index.php中的php使用require_once引用它,如下所示:

error_reporting(E_ALL);
ini_set('Display_errors','On');
require_once('../common/cachelite/Lite.php');

但抛出以下错误,我从Apache2 error.log中获取错误:

PHP Fatal error:  require_once(): Failed opening required '../common/cachelite/Lite.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/index.php on line 4

文件本身肯定存在,所以有人能为我解释这个问题吗?发生了什么事?

3 个答案:

答案 0 :(得分:1)

我已经遇到过这个问题几次并且总是像这样修复它。

$file = dirname(dirname(__file__));
require_once($file ."/common/cachelite/Lite.php");

进一步缩小范围。

试试这个 如果在网络服务器而不是命令行上添加< pre>< / pre>标签

echo __file__ . "\n";
echo dirname(__file__). "\n";
echo dirname(dirname(__file__)). "\n";

我的下一个想法是,文件权限或拼写错误的单词。

答案 1 :(得分:0)

您可以将代码更改为

require_once($_SERVER['DOCUMENT_ROOT'].'/common/cachelite/Lite.php');

或者你也可以这样:

require_once(dirname(__FILE__).'/common/cachelite/Lite.php');

答案 2 :(得分:0)

检查您尝试要求/包含的文件的权限。如果访问/ Web用户无法读取它们,那么这将解释您遇到的错误。

相关问题