2

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 ?

genesis
  • 50,477
  • 20
  • 96
  • 125
  • There is no such PATH environment variable like you are looking for. All Open Source applications I know define this path in a central configuration file, *but you have to include that configuration file first, using a relative path* – Pekka Jun 23 '11 at 11:15

6 Answers6

5

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
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • But if I'm creating it for customer or as opensource, I can't know what path does he use – genesis Jun 23 '11 at 11:03
  • @genesis then you need to use relative paths (`../`), there shouldn't be any fundamental problems with them? – Pekka Jun 23 '11 at 11:04
  • yes. When you are calling ../inc/include.php from /ajax/1/something.php, include.php will try to include /ajax/some_file.php – genesis Jun 23 '11 at 11:09
  • @genesis then you need to include `../../inc/include.php`. What's the problem? Not sure I understand. – Pekka Jun 23 '11 at 11:10
  • I'll describe it little bit more in my qeustion. Look above – genesis Jun 23 '11 at 11:16
  • Aah, didn't think about it. Thanks for an answer. +1, but wouldn't you mind to accept KingCrunch as he gave me, maybe easier answer and has less rep that you ? – genesis Jun 23 '11 at 11:23
  • @genesis sure, no problem at all. But I wouldn't use `getcwd()` for this. It's not 100% reliable (see the manual) and can be changed during runtime with `chdir()` – Pekka Jun 23 '11 at 11:26
  • I am not planning to do so, but I finally decided to use your answer. Thanks :) – genesis Jun 23 '11 at 11:29
  • No. http://php.net/manual/en/function.set-include-path.php It's just disabled if you don't have full control over the server, but saying that you can only get things from the "document root" is wrong. That's a webserver thing, PHP doesn't care about your document root. – phant0m Jun 23 '11 at 11:33
  • @phant0m what I am saying is that `$_SERVER["DOCUMENT_ROOT"]` is the only "root path" variable that you can get via PHP by default. PHP does not know the concept of an "application root" directory - you have to take care of defining and finding it out yourself. – Pekka Jun 23 '11 at 11:34
  • I'm not saying that, I'm saying that the include also takes into account the include path setting, which has no connection to `$_SERVER["DOCUMENT_ROOT"]` – phant0m Jun 23 '11 at 11:42
  • Note that `dirname(__FILE__)` will give something like `C:\wamp\www` on a Windows machine. – James P. Aug 28 '13 at 03:46
2

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';
KingCrunch
  • 128,817
  • 21
  • 151
  • 173
  • Nice, but exactly what I did before – genesis Jun 23 '11 at 11:06
  • @genesis: No, it isn't. If you use `../b.php` PHP resolves the path relative to the current work directory (see `getcwd()`). The magic constant `__FILE__` always points to the file itself, thus `dirname(__FILE__)` points to the directory of the file. At the end `dirname(__FILE__) . $relativeToCurrentFile` will result in an absolut path. Just dump it and look at it yourself ;) – KingCrunch Jun 23 '11 at 11:10
  • `echo dirname(__FILE__);` results in /var/www/path/to/app/inc oooh, getcwd() gave me correct answer! – genesis Jun 23 '11 at 11:20
  • please edit your answer to `echo getcwd()."/b.php";` and I'll accept your answer – genesis Jun 23 '11 at 11:21
  • include getcwd()."/b.php"; makes no sense and is (almost) identical to include "b.php"; (except for some preset include paths) – berkus Feb 17 '14 at 17:16
  • @genesis Actually `getcwd() . "/b.php";` is a security risk, because with this it is possible to include malicious code: Because the current work directory may change, it is possible, that this string refers to a completely different file. – KingCrunch Mar 03 '14 at 03:09
1

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';
sdolgy
  • 6,963
  • 3
  • 41
  • 61
  • it returns /var/www/path/to/my/app/inc - it'S not what I'm wanting to do – genesis Jun 23 '11 at 11:05
  • add the $GLOBALS declaration to your index.php before you call the include of subsequent scripts and add the require_once to your include.php – sdolgy Jun 23 '11 at 11:06
  • oh sorry. it works very well ! in inc/include.php I put `$path = dirname(__FILE__)."/../";include($path."b.php");` and it works! – genesis Jun 23 '11 at 11:27
1

Just use

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

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

Leif
  • 2,143
  • 2
  • 15
  • 26
1

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

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
1

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');
Mike
  • 21,301
  • 2
  • 42
  • 65