简体   繁体   English

PHP中的include()函数在XAMPP更新时的行为有所不同

[英]include() function in PHP behaves differently on XAMPP update

My files structure are : 我的文件结构是:

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

In my index.php file I wrote : 在我的index.php文件中,我写道:

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

and in includes/global.php I wrote : includes/global.php我写道:

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

the global.php in index.php will be include whitout any problem but config.php in global.php will not be include ! 如果没有任何问题, index.phpglobal.php将被包括在内,但global.php config.php将不被包含!

I use XAMPP and in my old version of XAMPP (version 1.7.3) I don't have any problem but today I installed new version of XAMPP (version 1.8.1) and I have this problem ! 我使用XAMPP,在旧版本的XAMPP(1.7.3版)中没有任何问题,但是今天我安装了新版本的XAMPP(1.8.1版),并且遇到了这个问题!

I should use absolute path for including config.php file in global.php but I don't want to use absolute path, I want to include like before, how can I do this ? 我应该使用绝对路径在global.php包含config.php文件,但我不想使用绝对路径,我想像以前一样包含在内,我该怎么做?

The path in include is always relative to the top-level script. include的路径始终是相对于顶级脚本的。 So your second (nested) include will not resolve correctly. 因此,您的第二个(嵌套)包含将无法正确解析。

A common workaround is to use: 常见的解决方法是使用:

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

inside includes/global.php . includes/global.php内部。 This will make the path resolve correctly, no matter where the toplevel script is located. 无论顶级脚本位于何处,这都将使路径正确解析。

Check your include_path . 检查您的include_path You need to add "." 您需要添加“。” to it. 对此。

Or you can define it in runtime. 或者,您可以在运行时定义它。

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

In

global.php,

write

include "includes/config.php";

I was having the same issue, but don't really want to use a workaround, so I tried a few things and discovered that the code I'm working on is written with old style php opening tag, ie <? 我遇到了同样的问题,但实际上并不想使用一种解决方法,因此我尝试了几件事,发现我正在处理的代码是用旧样式的php开始标记编写的,即<?

As soon as I changed it to <?php the includes worked. 一旦我将其更改为<?php ,includes就会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM