PHP correctly uses the "Application Root" in Xampp on Windows
I found a StackOverflow question on how to get the "app root" from my web app.
However, most of the approaches suggested in this thread can hardly be applied to my Xampp on Windows. Let's say I have "common.php" which is inside my web app directory:
/
/app/common.php
/protected/index.php
In my common.php that I have, like this:
define('DOCROOT', $_SERVER['DOCUMENT_ROOT']);
define('ABSPATH', dirname(__FILE__));
define('COMMONSCRIPT', $_SERVER['PHP_SELF']);
After I needed the common.php file inside /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 annoying part is the path separators which are not universal, plus it seems that all the superglobals from the $ _SERVER [] asso array like $ _SERVER ['PHP_SELF'] refer to the "calling" script, not to " the called "script.
It seems that I can only rely on dirname(__FILE__)
to make sure this always returns the absolute path to the common.php file.
I can, of course, parse the return values from DOCROOT and ABSPATH and then figure out the correct "application root". For example, I can compare the parts after htdocs
and replace all backslashes with a slash to get a unix-like path
I wonder if this is the correct way to handle this? What if I am deploying my web application in a LAMP environment? will this environmentally sensitive approach bomb me?
I've used some PHP frameworks like CakePHP and CodeIgniter to be frank. They just work with either LAMP or WAMP, but I'm not sure how they came up with such an elegant solution.
Thanks a lot in advance for all the tips and suggestions.
a source to share
dirname(__FILE__)
is a general method for obtaining a path. In fact, if your script is only going to run on PHP 5.3 or newer, __DIR__
will do the same and are slightly shorter for input.
I think your strategy involving common.php is a bit overkill. For starters, DOCROOT
and COMMONSCRIPT
useless - they just duplicate some data that is already global. (All PHP developers know what it is $_SERVER['PHP_SELF']
, but I personally wouldn't know what it means COMMONSCRIPT
without looking at it.)
Second, it can be more of a problem than it is worth. Compare several ways to get the path to /protected/protected2.php
from /protected/protected.php
:
ABSPATH . "/../protected/protected2.php";
"protected2.php"
dirname(__FILE__) . "/protected2.php";
They are all valid, but some are lighter than others. It just illustrates that what you are asking for (getting the root of the application) may not always be what you want. Therefore, I would say that there is no magic path bullet that will solve all your problems. If you find yourself in a situation where a constant helps you ABSPATH
, use it.
Second part of your question: Braid ingots will work on Windows, even mixed with backslashes. For instance:
include("c:\whatever\whatever/something/anything.php");
will work in PHP on Windows (at least any version 5.0+, not 4.x). Backslashes won't work on Unix, so always forward forward slashes and you'll be cross-platform.
If you are writing a cross platform script you never want to encode a drive letter. Instead, write it like this:
include("/whatever/whatever/something/anything.php");
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.)
NTN
a source to share