PHP get "Application Root" in Xampp on Windows correctly

Posted by Michael Mao on Stack Overflow See other posts from Stack Overflow or by Michael Mao
Published on 2010-05-09T00:52:58Z Indexed on 2010/05/09 0:58 UTC
Read the original article Hit count: 395

Filed under:
|
|

Hi all:

I found this thread on StackOverflow about how to get the "Application Root" from inside my web app.

However, most of the approaches suggested in that thread can hardly be applied to my Xampp on Windows. Say, I've got a "common.php" which stays inside my web app's app directory:

/
/app/common.php
/protected/index.php

In my common.php, what I've got is like this:

define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);
define('ABSPATH', dirname(__FILE__));
define('COMMONSCRIPT', $_SERVER['PHP_SELF']);

After I required the common.php inside the /protected/index.php, I found this:

C:/xampp/htdocs     //<== echo DOCROOT;
C:\xampp\htdocs\comic\app //<== echo ABSPATH
/comic/protected/index.php //<== echo COMMONSCRIPT

So the most troublesome part is the path delimiters are not universal, plus, it seems all superglobals from the $_SERVER[] asso array, such as $_SERVER['PHP_SELF'], are relative to the "caller" script, not the "callee" script.

It seems that I can only rely on dirname(__FILE__) to make sure this always returns an absolute path to the common.php file.

I can certainly parse the returning values from DOCROOT and ABSPATH and then calculate the correct "application root". For instance, I can compare the parts after htdocs and substitute all backslashes with slashes to get a unix-like path

I wonder is this the right way to handle this? What if I deploy my web app on a LAMP environment? would this environment-dependent approach bomb me out?

I have used some PHP frameworks such as CakePHP and CodeIgniter, to be frank, They just work on either LAMP or WAMP, but I am not sure how they approached such a elegant solution.

Many thanks in advance for all the hints and suggestions.

© Stack Overflow or respective owner

Related posts about php

Related posts about wamp