tags:

views:

222

answers:

1

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.

+1  A: 

dirname(__FILE__) is a common method of getting the path. Actually, if your script will only run on PHP 5.3 or later, __DIR__ will do the same thing and is a little shorter to type.

I think your strategy involving common.php is a little redundant. For starters, DOCROOT and COMMONSCRIPT are useless - they just duplicate some data that is already global. (All PHP developers know what $_SERVER['PHP_SELF'] is, but I personally would not know what COMMONSCRIPT means without looking it up.)

Second, it may be more trouble than it's worth. Compare some different ways of getting the path to /protected/protected2.php from /protected/protected.php:

ABSPATH . "/../protected/protected2.php";
"protected2.php"
dirname(__FILE__) . "/protected2.php";

All of these are valid, but some are easier than others. This just illustrates that what you are asking for (getting the application root) may not always be what you need. So I would say, there is no magic path bullet that will solve all of your problems. If you find yourself in a situation in which the ABSPATH constant helps you, use it.

Second part of your question: Forward slashes will work on Windows, even mixed with backslashes. For example:

include("c:\whatever\whatever/something/anything.php");

will work in PHP on Windows (at least on any 5.0+ version; not sure about 4.x). Backslashes will not work on Unix, so just always type forward slashes and you'll be cross-platform-safe.

If you are writing a cross-platform script, you never want to code in a drive letter. Instead, write it like this:

include("/whatever/whatever/something/anything.php");

The initial / will be interpreted by PHP on Windows as c:\. (I don't know how to point to a different drive letter in a cross-platform way.)

HTH

alexantd
@alexantd : thanks for your thorough explanation and good solution :)
Michael Mao