我在根目录中有一个 index.php 和一个包含 header.php 和 footer.php 的“includes”文件夹。我在 index.php 中包含页眉和页脚并且它工作正常但是在页眉和页脚中我包含了一些不同的文件根目录下的config文件夹。
我知道我应该使用“../config/config.php”,但它适用于“config/config.php”。
我在 index.php 中包含了页眉和页脚,我应该根据我包含在 ??? 中的索引使用路径
如果我在多个文件夹中包含一个文件怎么办???
最佳答案
尝试使用 __DIR__
它会给你与当前文件所在位置相比的路径,也尝试使用 include_once
而不是 include
DIR : The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(FILE). This directory name does not have a trailing slash unless it is the root directory.
在 header.php 和 footer.php 中包含这样的文件:
include_once DIR.'/../config/config.php';
对其他文件做同样的技术
关于php - 包含在 PHP 中的路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45529992/