PHP中的include()函数在XAMPP更新时表现不同

时间:2013-04-26 10:44:04

标签: php path include xampp

我的文件结构是:

/ (Root)
|-- includes
|-- |-- global.php
|-- |-- config.php
|-- index.php

在我写的index.php文件中:

<?php
include "includes/global.php";
?>

并在includes/global.php我写道:

<?php
include "config.php";
?>

global.php中的index.php将包括whitout任何问题,但config.php中的global.php将不包括在内!

我使用XAMPP和我的旧版XAMPP(版本1.7.3)我没有任何问题,但今天我安装了新版本的XAMPP(版本1.8.1),我遇到了这个问题!

我应该使用绝对路径在config.php中包含global.php文件,但我不想使用绝对路径,我想像以前一样包含,我该怎么做呢?

4 个答案:

答案 0 :(得分:1)

include中的路径始终相对于顶级脚本。所以你的第二个(嵌套)包含将无法正确解析。

常见的解决方法是使用:

<?php
include dirname(__FILE__)."/config.php";
?>

includes/global.php内。无论顶层脚本位于何处,这都将使路径正确解析。

答案 1 :(得分:1)

检查include_path。你需要添加“。”它。

或者您可以在运行时定义它。

set_include_path(get_include_path() . PATH_SEPARATOR . '.');

答案 2 :(得分:0)

global.php,

include "includes/config.php";

答案 3 :(得分:0)

我遇到了同样的问题,但是我真的不想使用变通方法,所以我尝试了一些事情并发现我正在处理的代码是用旧式的php开启标记编写的,即{{1 }}

只要我将其更改为<?,就可以使用包含功能。