gen*_*sis 2 php path relative-path include absolute-path
很抱歉,因为它可能会被多次回答,但我的问题有点不同
我喜欢树
/var/www/path/to/my/app/
-- index.php
-- b.php
-- inc/
-- include.php
Run Code Online (Sandbox Code Playgroud)
(我正在从index.php访问inc/include.php,
include "inc/include.php";
Run Code Online (Sandbox Code Playgroud)
)
但是在include.php中,我需要获取APPLICATION根的绝对路径,而不是DOCUMENT_ROOT所以在结果中,我需要能够使用这个命令
//inc/include.php
include(APP_ROOT."/b.php");
Run Code Online (Sandbox Code Playgroud)
重复,我不想打电话给他
include("../b.php");
Run Code Online (Sandbox Code Playgroud)
如果可能的话,有原生功能吗?
我想通过PHP获取PATH,因为任何开源需要有这个路径检测的原因?如果我想要包含来自ajax/1/somethiing.php的inc/include.php,那么成功但inc/include.php会尝试包含ajax/b.php而不是b.php
我有这棵树
-- index.php
-- b.php
-- inc/
-- include.php
-- ajax/
-- 1/
-- ajax.php
Run Code Online (Sandbox Code Playgroud)
现在看.从index.php开始,您将调用inc/include.php
include("inc/include.php"); //included file
Run Code Online (Sandbox Code Playgroud)
现在,包含文件搜索
include("../b.php");
Run Code Online (Sandbox Code Playgroud)
它会起作用,但是!如果我从ajax/1/ajax.php调用include of inc/include.php,就像这样
include("../../inc/include.php");
Run Code Online (Sandbox Code Playgroud)
它会工作,但包含文件将尝试包括
../b.php
Run Code Online (Sandbox Code Playgroud)
代替../../b.php作为路径是相对于包含inc/include.php的文件得到了吗?
如果可能的话,有原生功能吗?
没有.文档根是您可以从PHP获得的唯一内容.(但请注意,在您的方案中,您可以简单地调用,include("b.php");因为脚本仍在index.php的上下文中.)
重新更新:
您可以在中央配置文件中定义全局应用程序根目录.假设您config.php的应用程序根目录.然后做一个
define("APP_ROOT", dirname(__FILE__));
Run Code Online (Sandbox Code Playgroud)
您仍然必须包含配置文件,并且必须使用它的相对路径,例如
include ("../../../config.php");
Run Code Online (Sandbox Code Playgroud)
但是一旦你这样做了,你可以相对于脚本中的app root工作:
include (APP_ROOT."/b.php"); <-- Will always return the correct path
Run Code Online (Sandbox Code Playgroud)