Definitive way of obtaining the root directory.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
eazyGen
Forum Commoner
Posts: 46
Joined: Mon Aug 29, 2011 4:32 am
Location: Central London

Definitive way of obtaining the root directory.

Post by eazyGen »

Hi guys,

I am searching for the definitive way of obtaining the root directory of my app. I need to it to work on localhost and also on ALL platforms, NIX, Windows etc. I would like to do this as I wish to use it to locate all my sub directory files - includes, model etc.

For example, I may have this as my landing page:

http://www.mywebsite.com/index.php

The root directory might be:

html

The index.php might reside here:

html/index.php

... but my includes files might be here:

html/lib/includes

So I would be looking to obtain the root in an absolute kind of way to allow me to string together the includes sub directory in a relative kind of way.

In summary then:

1. How to obtain the root with 100% percent success, all of the time on all platforms.
2. How to string the sub directory together with 100% percent success, all of the time on all platforms.
3. Any comments on this as an approach, also welcome.

Many thanks in advance,

S
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Definitive way of obtaining the root directory.

Post by Apollo »

1: $_SERVER['DOCUMENT_ROOT']

2 and 3: It is perfectly possible (and from a security point of view perhaps even favorable*) to put libraries, include files, etc outside the document root. So merely scanning inside the root is definitely not guaranteed to locate all files involved in your website.

(* because it makes sure those library and include files are not directly accessible from outside, amongst other reasons)
Post Reply