__DIR__ 不适用于 php 包含

2022-01-25 00:00:00 include php

我正在尝试使用 php 包含将一个 .php 文件链接到另一个.结构是这样的:

I am trying to use php includes to link one .php file to another. The structure is this:

ROOT
¦
+---templates
¦   ¦
¦   +---footer.php
¦
¦
+---main
    ¦
    +---maps
        ¦
        +---uk
            ¦
            +---map.php

使用 代码链接绝对没问题.

Using <?PHP include "../../../templates/navbar.php"; ?> the code links absolutely fine.

使用 <?PHP include dirname(__FILE__) .../../../templates/navbar.php";?>

让我一无所获.我错过了什么?谢谢.

or <?PHP include __DIR__ . "../../../templates/navbar.php"; ?> gets me absolutely nothing. What am I missing? Thanks.

推荐答案

__DIR__ 在目录名中不包含尾随的 /,所以 <?PHP 包含 __DIR__ ."/../../../templates/navbar.php";?> 是你需要的.

__DIR__ doesn't include the trailing / in directory name, so <?PHP include __DIR__ . "/../../../templates/navbar.php"; ?> is what you need.

相关文章