【发布时间】:2017-12-10 08:42:37
【问题描述】:
我有这种情况:
/
index.php
/init (folder)
init.php (of course inside this file i use require_once for db/function/and whatever you want to put inside)
/layout_parts (folder)
header.php (and others common parts are inside this folder)
/my_space (folder inside layout parts)
my_file.php
another_file.php
/your_space (folder inside layout parts)
your_file.php
another_file.php
/third_space (folder inside layout parts)
third_file.php
another_file.php
对于索引,我没有问题,一切正常,但如果我 include header.php 在其中一个子文件夹文件中,我在 header.php 顶部的 require_once(init/init.php); 将不起作用。
(警告消息说目录中没有这样的文件(当然文件存在),它记下不是我预期的路径的子文件夹目录。
我尝试echo $SERVER['DOCUMENT_ROOT];查看整个路径,echo __DIR__;查看该目录的正确路径,没有出路。
我知道这是一个愚蠢的问题,但如果有好心人可以帮助我,那就太好了。谢谢:)
【问题讨论】:
-
../将此添加到您的include或require_once()文件路径中,例如:-../init/init.php -
是的,我已经尝试过这种方式,它只是警告同样的事情只是在路径之前添加 ../... :'(
-
重要的是要记住,如果您包含/需要一个包含/需要其他文件的文件,所有相对路径都将是相对于执行包含的文件,而不是文件本身。最好的办法是:
require __DIR__ . '/relative/from/this/file.php。 DIR 为您提供写入文件的绝对路径。 -
您所拥有的问题是,在 layout_parts 中您需要
../在 my_space 中您将需要../../等等。
标签: php path include subdirectory require-once