2

I have searched many threads so far but cant seem to find a solution. Inside one of my php scripts I am trying to get a server document root but the value I get is not complete, its simply missing the domain folder. I believe it is due to sharing hosting or smth else.

Here is the current way I am using:

$root = realpath($_SERVER["DOCUMENT_ROOT"]);

and the path I get is like:

/home/content/01/0151247/html

although I know it should be like:

/home/content/01/0151247/html/mydomain

I know as I compared it with SCRIPT_NAME and I see the mydomain there in the path.

Hope someone could direct me.

Thank you and sorry for probably asking another thousand time same question over community, I really tried things around from here, nothing helps me so far.

UPDATE

unfortunately I cant not simply use my index file with DIR as it is a wordpress setup and I am working on a separate folder where I am including some wordpress functionality but for that I need a document_root. If that would help.

UPDATE

apparantly the following way resolved my case, maybe it will help someone one day:

realpath($_SERVER["SUBDOMAIN_DOCUMENT_ROOT"]);

basically because of the server setup and domain configured as a subdomain.

Thanks to all who participated.

devjs11
  • 1,898
  • 7
  • 43
  • 73
  • It would help if you gave a more full explanation of the requirements and constraints. Which files need to know the webroot and where are they located, etc. Side Note: The value inside `$_SERVER['DOCUMENT_ROOT']` **_is_** your document root. The document root is not always the web root. – Shad Dec 06 '11 at 01:14
  • I apologize for lack of details due to my beginner level of php and server sider programming in general. Did my best anyway. A big thanks to @Shad as I did learn quite a few things tonight! Thank you. – devjs11 Dec 06 '11 at 01:55
  • Thanks ;). If you find something yourself that works, go ahead and submit it as an answer to your question and accept it =) – Shad Dec 06 '11 at 01:56

3 Answers3

3

Prior to PHP 5.3 you can put a file in the directory whose path you want and define a constant:

define('ROOT_DIR', dirname( __FILE__ ));

After 5.3 you can just do:

define('ROOT_DIR', __DIR__);

The idea being that this would be in config.php of some sort that is included every time the application runs.

Magic Constants Docs

Shad
  • 15,134
  • 2
  • 22
  • 34
  • This is clean and beautiful, but unfortunatly I am under wordpress install. I have a custom script inside my theme for so reason I can not simply use __FILE__ as I probably have no way to include that at first level with index. – devjs11 Dec 05 '11 at 22:38
  • @Alex what does your `ABSPATH` constant resolve to? (after including the WP bootstrap) – Shad Dec 05 '11 at 23:42
  • Thank you for getting back to me. If i got you right, I tried to include wp bootstrap like that require_once("/home/content/01/0151247/html/mydomain/wp-load.php"); and then echo ABSPATH; In that case I do get returned the correct path /home/content/01/0151247/html/mydomain as needed. Did that help? – devjs11 Dec 06 '11 at 00:48
  • @Alex Can you rely on `ABSPATH` instead of `DOCUMENT_ROOT`? Since you are including WP functionality, perhaps you could rely on WP's Constant in this case~? – Shad Dec 06 '11 at 01:01
  • Yes I could but i can get it only if I manually type in the document_root to wp-load.php and I cant afford it as it will change on other servers. – devjs11 Dec 06 '11 at 01:05
  • @Alex I think I need more info =) – Shad Dec 06 '11 at 01:14
  • Great News! Just found a solution. realpath($_SERVER["SUBDOMAIN_DOCUMENT_ROOT"]); perfectly returns what is needed. I saw it on phpinfo(); Please confirm if that makes sense and safe to use? – devjs11 Dec 06 '11 at 01:39
  • @Alex If you are always going to use the same server setup then go ahead, but I've never seen that _SERVER var before myself. – Shad Dec 06 '11 at 01:42
1

UPDATE

In the config file, you can just append the DOCUMENT_ROOT variable:

$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/mydomain';

And that should take care of it for you.

Old Solution

The DOCUMENT_ROOT is an environment variable set by the server. So if this is on shared hosting, you cannot changed. An alternative is to set your own constant to it, so in a config type file that is included on your pages you can do something like:

define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT'] . '/mydomain');

And then just use that constant in place of $_SERVER['DOCUMENT_ROOT']. The other option is to contact your host and inquire about it, maybe it was an oversight on their part and they will fix it.

EDIT

Probably using the __DIR__ as others have posted about is the better way, as the DOCUMENT_ROOT can be set to different items and at least with the __DIR__ you should get an accurate directory each time.

Jim
  • 18,673
  • 5
  • 49
  • 65
  • unfortunately I cant not simply using my index file with __DIR__ as it is a wordpress setup and I am working on a separate folder inside the domain from inside which I am including some wordpress functionality but for that I need a document_root – devjs11 Dec 05 '11 at 22:14
  • @Alex Well you can just modify the `$_SERVER['DOCUMENT_ROOT']` variable. Updated my answer. – Jim Dec 05 '11 at 22:15
  • Actually to manually define '/mydomain' in the string is also not an option as it my change tomorrow :( – devjs11 Dec 05 '11 at 22:17
  • @Alex, If the `__FILE__` echos our the right directory you need, then just do `$_SERVER['DOCUMENT_ROOT'] = dirname(__FILE__);` and that should work out pretty good. – Jim Dec 05 '11 at 22:20
  • in my case __FILE__ returns the directly where the script is located. I dont have any include to refer to index page. Would it still be possible to detect the root in my case? – devjs11 Dec 05 '11 at 22:36
0

Personally, to get the root of a folder in PHP, I use this in the my index file:

define('ROOT', dirname(__FILE__));     // __DIR__ will work under PHP 5.3
jValdron
  • 3,408
  • 1
  • 28
  • 44