PHP:是否可以包含兄弟目录中的文件?
问题描述:
嗨所以在我的主家目录我有2个子目录:subDir_A和subDir_B。在subDir_A有一个名为file_a.php。在subDir_B我有一个文件file_b.php的文件。在file_b.php我想在包括file_a.php。它是否可能?我正在使用Xamp。 PHP:是否可以包含兄弟目录中的文件?
答
是的。
在file_B.php:include('../subDir_A/file_A.php');
的../
意味着你要上去一个目录。
答
使用4种功能之一很容易完成。
include
,include_once
,require
或require_once
,请参阅本http://php.net/manual/en/function.include.php文档(我不会在此展开,因为它是非常基本的PHP功能)
你应该做你包括以下方式。
# you could use any of the other keywords as well.
include __DIR__ . '/../subDir_A/file_A.php';
魔术常量__DIR__
使用,因为它会考虑相对的参考,它被称为在文件中。如果你不这样做,只是做include '../subDir_A/file_A.php';
,如果你提供file_B.php别的地方可能无法正常工作因为它将与调用它的父文件相关。
include_once(../ subDir_A/file_A.php) –