Jump to content

Definitive way of obtaining the root directory.


eazyGen

Recommended Posts

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

Link to comment
Share on other sites

You might find what you are looking for here

Particularly, the DOCUMENT_ROOT index.

Many thanks. However, this seems to highlight the problem I am having and the reason for my post.

 

The link has two statements that concern me:

 

$HTTP_SERVER_VARS [deprecated]

 

... so I cannot use that.

 

"$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these ..."

 

I am loathe therefore to pursue a solution that carries no guarantees.

 

Any further thoughts much appreciated.

 

S

Link to comment
Share on other sites

The only way you are ever going to guarentee anything is if you have direct admin access to the server.  Basicly, the software can only get the information that the server will let it access, unless you controll that server, there can be no guarantees.

If that is the case, and I must say I find it slightly odd that something so apparently simple should not come with a guarantee, is there a "best practice" to achieve what I am aiming for? Perhaps a settings.ini file???

 

S

Link to comment
Share on other sites

DOCUMENT_ROOT should always be set. If it isn't set, someone didn't set up IIS to work nicely with PHP ;) or you're running PHP through the CLI

 

Here's a solution:

$doc_root = str_replace('\\', '/',
substr(
	$_SERVER['SCRIPT_FILENAME'],
	0, 0-strlen($_SERVER['PHP_SELF'])
)
);

 

From http://php.net/manual/en/reserved.variables.php#63831

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.