require_once发出警告和致命错误

时间:2013-10-31 10:54:36

标签: php file include require-once

public_html > config.php 

public_html > A > test_file.php

在test_file.php代码中

require_once("../config.php");

工作正常。 在Dreamweaver中显示动态相关的文件(config.php)

public_html > includes > classes > allclasses.php

in allclasses.php code

require_once("../../config.php");

在Dreamweaver中显示动态相关文件(config.php)

当我在test_file.php

中包含allclasses.php时
require_once("../config.php");
require_once("../includes/classes/allclasses.php");

在Dreamweaver中显示动态相关文件(config.php,allclasses.php)

test_file.php停止工作并在浏览器上显示

Warning: require_once(../../config.php) [function.require-once]: 
failed to open stream: No such file or directory in .....\public_html\includes\classes\allclasses.php on line 11

使用xampp作为开发服务器。

3 个答案:

答案 0 :(得分:2)

当包括内部包含时,最好使用绝对路径。因此,您可以使用__DIR____FILE__常量来执行此操作,就像在test_file.php中一样:

require_once(realpath(dirname(__FILE__) . '/../config.php'));

或只是

require_once(realpath(__DIR__ . '/../config.php'));

答案 1 :(得分:0)

使用绝对网址路径而不是相对网址路径。

试试这个:

require_once($_SERVER["DOCUMENT_ROOT"]."/config.php");

答案 2 :(得分:0)

除非您调用类似chdir()的内容,否则所有require / include调用都与被调用PHP文件的目录相关(此处为 test_file.php )。这就是 allclasses.php 中的require_once("../../config.php")引发错误的原因。

在您的情况下,最简单的解决方法是从 allclasses.php 中移除require_once("../../config.php"),如果它没有其他影响。

我建议使用 front controller ,以确保被调用的PHP脚本始终是同一个文件(并且始终为同一目录创建include / require),并且对于 config.php 文件的所有内容,只执行一次。