简体   繁体   English

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

[英]Absolute (or relative?) path in PHP

Sorry for asking it as it may be answered many times before, but my question is little bit different 很抱歉,因为它可能会被多次回答,但我的问题有点不同

I have tree like 我喜欢树

/var/www/path/to/my/app/
   -- index.php
   -- b.php
   -- inc/
      -- include.php

(I'm accessing inc/include.php from index.php, (我正在从index.php访问inc / include.php,

include "inc/include.php";

)

but in include.php, I need to get absolute path to APPLICATION root, not DOCUMENT_ROOT so in result, I need to be able to use this command 但是在include.php中,我需要获取APPLICATION根的绝对路径,而不是DOCUMENT_ROOT所以在结果中,我需要能够使用这个命令

//inc/include.php
include(APP_ROOT."/b.php");

repeating, I do NOT WANT TO CALL IT LIKE 重复,我不想打电话给他

include("../b.php");

is there native function, if possible? 如果可能的话,有原生功能吗?

UPDATE: 更新:

I am wanting to get PATH by PHP as any open source need to have this path detection why? 我想通过PHP获取PATH,因为任何开源需要有这个路径检测的原因? If I would want to include inc/include.php from ajax/1/somethiing.php, it success but inc/include.php then tries to include ajax/b.php instead of b.php 如果我想要从ajax / 1 / somethiing.php中包含inc / include.php,那么成功但inc / include.php会尝试包含ajax / b.php而不是b.php

For Pekka: 对于佩卡:

I have this tree 我有这棵树

-- index.php
-- b.php
-- inc/
    -- include.php
-- ajax/
    -- 1/
       -- ajax.php

Now look. 现在看。 From index.php, you'll call inc/include.php 从index.php开始,您将调用inc / include.php

include("inc/include.php"); //included file

now, included file searchs for 现在,包含文件搜索

include("../b.php");

It will work, but! 它会起作用,但是! If I would call include of inc/include.php from ajax/1/ajax.php, like this 如果我从ajax / 1 / ajax.php调用include of inc / include.php,就像这样

include("../../inc/include.php");

it will work, but included file will try to include 它会工作,但包含文件将尝试包括

../b.php 

instead of ../../b.php as path is relative to file which INCLUDED that inc/include.php Got it ? 代替../../b.php作为路径是相对于包含inc / include.php的文件得到了吗?

is there native function, if possible? 如果可能的话,有原生功能吗?

no. 没有。 The document root is the only thing you can get from PHP. 文档根是您可以从PHP获得的唯一内容。 (Note however that in your scenario, you can simply call include("b.php"); because the script is still in the context of index.php.) (但请注意,在您的方案中,您只需调用include("b.php");因为脚本仍在index.php的上下文中。)

Re your update: 重新更新:

You can define a global application root in a central configuration file. 您可以在中央配置文件中定义全局应用程序根目录。 Say you have config.php in your app root. 假设您的app根目录中有config.php Then do a 然后做一个

define("APP_ROOT", dirname(__FILE__));

you still have to include the config file, and you have to use relative paths for it, eg 仍然必须包含配置文件,并且必须使用它的相对路径,例如

include ("../../../config.php");

but once you have done that, you can work relative to the app root inside the script: 但是一旦你这样做了,你可以相对于脚本中的app root工作:

include (APP_ROOT."/b.php");  <--  Will always return the correct path

You can resolve the path with the current file as the base 您可以使用当前文件作为基础来解析路径

include dirname(__FILE__) . DIRECTORY_SEPARATOR . '/../b.php';

or since PHP5.3 或者从PHP5.3开始

include __DIR__ . \DIRECTORY_SEPERATOR . '/../b.php';

Add to your index.php: 添加到index.php:

$GLOBALS['YOUR_CODE_ROOT'] = dirname(__FILE__);

Add to your inc/include.php : 添加到您的inc/include.php

require_once $GLOBALS['YOUR_CODE_ROOT'].'/b.php';

Just use 只是用

define('APP_ROOT', 'your app root');

in index.php for example. 例如,在index.php中。 Or in a config file. 或者在配置文件中。

虽然它没有为您定义应用程序的根路径,但可能有必要查看set_include_path()

If you only know the relative path, then you have to at least start with a relative path. 如果您只知道相对路径,那么您必须至少从相对路径开始。 You could use realpath to return the canonicalized absolute pathname, and then store that somewhere. 您可以使用realpath返回规范化的绝对​​路径名,然后将其存储在某处。

In your index.php , or a config file: index.php或配置文件中:

define(
    'INC',
    realpath(dirname(__FILE__)) .
    DIRECTORY_SEPARATOR .
    'inc' .
    DIRECTORY_SEPARATOR
);

Elsewhere: 别处:

include(INC . 'include.php');

Alternatively, define a few constants for the different locations: 或者,为不同的位置定义一些常量:

define('DOCUMENT_ROOT', realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR);
define('INC', DOCUMENT_ROOT . 'inc' . DIRECTORY_SEPARATOR);
define('AJAX', DOCUMENT_ROOT . 'ajax' . DIRECTORY_SEPARATOR);

Elsewhere: 别处:

include(DOCUMENT_ROOT . 'b.php');
include(INC . 'include.php');
include(AJAX . 'ajax.php');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM