PHP的绝对(或相对?)路径

| 很抱歉要求您回答,因为之前可能已经回答了很多次,但是我的问题有点不同 我有树
/var/www/path/to/my/app/
   -- index.php
   -- b.php
   -- inc/
      -- include.php
(我正在从index.php访问inc / include.php,
include \"inc/include.php\";
) 但是在include.php中,我需要获取到APPLICATION根目录的绝对路径,而不是DOCUMENT_ROOT 因此,我需要能够使用此命令
//inc/include.php
include(APP_ROOT.\"/b.php\");
重复,我不想打电话给我
include(\"../b.php\");
是否有本机功能? 更新: 我想通过PHP获取PATH,因为任何开源都需要进行此路径检测 为什么?如果我想包含ajax / 1 / somethiing.php中的inc / include.php,它会成功,但是inc / include.php然后尝试包含ajax / b.php而不是b.php 对于Pekka: 我有这棵树
-- index.php
-- b.php
-- inc/
    -- include.php
-- ajax/
    -- 1/
       -- ajax.php
现在看。在index.php中,您将调用inc / include.php
include(\"inc/include.php\"); //included file
现在,包含的文件搜索为
include(\"../b.php\");
它将起作用,但是!如果我从ajax / 1 / ajax.php调用inc / include.php的include,就像这样
include(\"../../inc/include.php\");
它将起作用,但是包含文件将尝试包含
../b.php 
代替     ../../b.php作为路径是相对于包含inc / include.php的文件的 得到它了 ?     
已邀请:
           是否有本机功能? 没有。您只能从PHP获得文档根目录。 (但是请注意,在您的情况下,您可以简单地调用
include(\"b.php\");
,因为脚本仍位于index.php的上下文中。) 重新更新: 您可以在中央配置文件中定义全局应用程序根。假设您的应用程式根目录有
config.php
。然后做一个
define(\"APP_ROOT\", dirname(__FILE__));
您仍然必须包含配置文件,并且必须使用相对路径,例如
include (\"../../../config.php\");
但是一旦完成,就可以在脚本中相对于应用程序根目录进行工作:
include (APP_ROOT.\"/b.php\");  <--  Will always return the correct path
    
        您可以使用当前文件作为基础来解析路径
include dirname(__FILE__) . DIRECTORY_SEPARATOR . \'/../b.php\';
或从PHP5.3起
include __DIR__ . \\DIRECTORY_SEPERATOR . \'/../b.php\';
    
        只需使用
define(\'APP_ROOT\', \'your app root\');
例如在index.php中。或在配置文件中。     
        虽然它没有为您定义应用程序的根路径,但可能值得关注to17     
        添加到您的index.php:
$GLOBALS[\'YOUR_CODE_ROOT\'] = dirname(__FILE__);
添加至您的
inc/include.php
require_once $GLOBALS[\'YOUR_CODE_ROOT\'].\'/b.php\';
    
        如果只知道相对路径,则至少必须从相对路径开始。您可以使用realpath返回规范化的绝对​​路径名,然后将其存储在某个位置。 在您的
index.php
或配置文件中:
define(
    \'INC\',
    realpath(dirname(__FILE__)) .
    DIRECTORY_SEPARATOR .
    \'inc\' .
    DIRECTORY_SEPARATOR
);
别处:
include(INC . \'include.php\');
或者,为不同的位置定义一些常量:
define(\'DOCUMENT_ROOT\', realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
define(\'INC\', DOCUMENT_ROOT . \'inc\' . DIRECTORY_SEPARATOR);
define(\'AJAX\', DOCUMENT_ROOT . \'ajax\' . DIRECTORY_SEPARATOR);
别处:
include(DOCUMENT_ROOT . \'b.php\');
include(INC . \'include.php\');
include(AJAX . \'ajax.php\');
    

要回复问题请先登录注册