嵌套include_once问题

pot*_*mic 1 php

这是我的目录树:

  • /
    • 的index.php
  • 包括/
    • 的functions.php
    • head.php
    • connect.php
  • 子/
    • 的index.php

在我的head.php和connect.php都有:

include_once 'include/functions.php';
Run Code Online (Sandbox Code Playgroud)

我在根文件夹中的index.php包括以下两个:head.php和connect.php,如下所示:

include_once 'include/connect.php';
include_once 'include/head.php;'
Run Code Online (Sandbox Code Playgroud)

但是,当sub /中的index.php包含functions.php和head.php时,它们将无法包含functions.php.这是我在sub/index.php中包含的内容:

include_once '../include/connect.php';
include_once '../include/head.php';
Run Code Online (Sandbox Code Playgroud)

如果我将head.php和connect.php更改为:include_once'../include/functions.php';

sub/index.php通常会包含所有内容,但root中的index.php将无法加载functions.php.

我怎样才能解决这个问题?

PHP版本:5.2.*

Sch*_*eis 5

__DIR__在include语句中使用常量,然后相对于它移动.所以在sub/index.php中你会这样做include_once __DIR__ . '../include/connect.php'

__DIR__是一个常量,它是您所在文件的目录.

http://php.net/manual/en/language.constants.predefined.php

如果您使用的是php <v5.3,则可以使用它dirname(__FILE__)来获得相同的内容.

  • 请参阅更新,您可以使用`dirname(__ FILE __)`来获取以前php版本中的目录 (2认同)