I'm sure it's possible to simplify the following two lines, but my brain is not up to speed this morning, can anyone help? Code: $root_url = str_replace($_SERVER['DOCUMENT_ROOT'], '', realpath(str_replace(basename(__FILE__), '', __FILE__).'/..')); $root_dir = $_SERVER['DOCUMENT_ROOT'].$root_url;
It might be but I can't see what you're after... what is it trying to do? What is the first line supposed to be doing? Here's what it seems to be doing to me (I expanded this out a bit) Code: // removes the path part of a filename (/var/www/mypage.com/stuff/page.php -> page.php) $w = basename(__FILE__); /* replace page.php with "/.." in /var/www/mypage.com/stuff/page.php -> /var/www/mypage.com/stuff//.. * nb could just use .. not /.. ? */ $x = str_replace($w, '', __FILE__).'/..' // cleans it up to an actal path (/var/www/mypage.com/stuff//.. -> /var/www/mypage.com/stuff/.. -> /var/www/mypage.com/) $y = realpath($x); /* remove the document root (/var/www/www.mypage.com) -> empty string if document root is the same * does nothing if they are different */ $root_url = str_replace($_SERVER['DOCUMENT_ROOT'], '', $y); So you'll either have '' or something like /var/www/www.mypage.com/ if the server document root and the file path differ
Why I need these lines: I include this code (in a file) in various files in various locations. I need $root_url for relative URLs, and $root_dir for file include paths. The system I'm building may be deployed to various servers and hence in various folders. It does indeed appear that they're flawed.
My approach: PHP: //in files that need this data:include 'static.php';//in static.phpdefine('_BASE_SITE_URL','http://localhost/mct/'); Maybe not quite as infinitely flexible, but a whole heck of a lot easier and I expect easier on resources as well. Make a couple defines in static.php for different _BASE_SITE_URL and comment out all but one if you've got a local and a remote version of the site.
The problem is that the system is updated to various locations via CVS. I just figure there must be someway of writing the code I need, and it's eluding me...
of course, but it doesn't help me with the variable locations issue. a different version I've just dug up: Code: define('FILE_ROOT', realpath('.').'/'); $DIR_PHP_SELF = dirname($_SERVER['PHP_SELF']); $SITE_ROOT = $DIR_PHP_SELF == '/' || $DIR_PHP_SELF == '\\' ? '' : dirname($_SERVER['PHP_SELF']); define('SITE_ROOT', $SITE_ROOT); I'm surprised that I've never found a pre-written solution, it seems to me like a common requirement.
you could always explode() the $_SERVER['HTTP_HOST'] by a slash, count the number of elements in the resulting array, and pull the appropriate one(s) into a new variable.