Problem
Multiple development sites on a single server housed in varying subfolders off the webroot, each with it's own includes directories. When using include or require with URL file-access disabled you must supply either the complete real or relative path. Example Heirarchy:
/mainsite/
/mainsite/inc
/mainsite/website1
/mainsite/website1/inc
/mainsite/website2
/mainsite/website2/inc
Possible Solution?
I've tried half a dozen solutions to mixed success from Redirects to parsing or splitting the URI to find the document root. However, when you change the folder depth and hosts as often as I do (usually moving between servers, both apache and IIS for ease of development) it wreaks havoc upon almost every solution I've tried. The closest solution I found was:
PHP Code:
$absPATH = !isset($_SERVER['DOCUMENT_ROOT']) ? str_replace( '\\', '/', substr($_SERVER['SCRIPT_FILENAME'],0,0-strlen($_SERVER['PHP_SELF']))).'/' : $_SERVER['DOCUMENT_ROOT'] ;
This solution only gives me the "root" folder for the server, and fails to account for depth/heirarchy changes. So I came up with this:
PHP Code:
// ### Inverse Recursive Search for "Robots.txt" for($relPATH = './'; !is_file($relPATH.'robots.txt'); $relPATH .= '../') {} $absPATH = realpath($relPATH); // ### $relPATH = Relative path to the current site's document root. // ### $absPATH = Absolute/Real path to the current site's document root.
Obviously you'd want to add a little extra criteria to keep this loop from running away (If you didn't have a robots.txt file for example). While this solution is reliant upon the exisitence of a robots.txt file, this is just about the only reliable cross-platform method I could think of.
Questions
Any reasons/situations why/where this wouldn't/shouldn't be used?
Is there a better way?
Thanks in advance.
Last edited by Envision_frodo; 01-13-2009 at 01:39 PM..
Reason: Forgot to Mention
|