Absolute (or relative?) path in PHP

2019-04-13 06:38发布

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,

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

//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? 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

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

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

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 ?

6条回答
萌系小妹纸
2楼-- · 2019-04-13 06:59

Add to your index.php:

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

Add to your inc/include.php:

require_once $GLOBALS['YOUR_CODE_ROOT'].'/b.php';
查看更多
趁早两清
3楼-- · 2019-04-13 07:08

While it doesn't define your application's root path for you, it might be of interest to check out set_include_path()

查看更多
Viruses.
4楼-- · 2019-04-13 07:14

is there native function, if possible?

no. The document root is the only thing you can get from 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.)

Re your update:

You can define a global application root in a central configuration file. Say you have config.php in your app root. 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, e.g.

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

but once you have done that, you can work relative to the app root inside the script:

include (APP_ROOT."/b.php");  <--  Will always return the correct path
查看更多
Anthone
5楼-- · 2019-04-13 07:14

You can resolve the path with the current file as the base

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

or since PHP5.3

include __DIR__ . \DIRECTORY_SEPERATOR . '/../b.php';
查看更多
放荡不羁爱自由
6楼-- · 2019-04-13 07:17

Just use

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

in index.php for example. Or in a config file.

查看更多
smile是对你的礼貌
7楼-- · 2019-04-13 07:19

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.

In your index.php, or a config file:

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');
查看更多
登录 后发表回答