The PATH_TRANSLATED blues
Wondering if anyone can provide some help / tips for a problem we’ve encountered in WACT. Can imagine other people are having the same problem.
Basically we need a way to determine that path of the script where execution began from within WACT.
Typically when using WACT you’d begin a script like;
Code within WACT then needs to be able to determine where myscript.php is located in the filesystem, so that a users templates, for example, can be located relative to myscript.php’s path.
This may seem like a simple problem to solve, by simply using;
$myscript_path = dirname($_SERVER['SCRIPT_FILENAME']);
Unfortunately this only works on some PHP configurations – when PHP is used as a CGI executable (and probably the same for the CLI variant) contains the path to the PHP executable, not the PHP script where execution began.
An alternative option which seemed to look good for a short time was;
$myscript_path = dirname($_SERVER['PATH_TRANSLATED']);
Looking at the PHP bugs list though, there seem to have been issues with this variable, relating to the Apache version being used. Worse news is it’s been completely dropped from PHP5, mentioned here.
Any other ideas for how to reliably determine the path of the script where execution began? Beginning to wonder if PHP needs a predefined constant such as __MAIN__ which handles this independent of PHP install.
Jeff highlights the problem in more detail here.