I inherited sites where previous developer used the spaghetti code below to handle relative paths for nav, image, and script items.
if(substr_count($_SERVER['REQUEST_URI'], "/") <= 1){
define('NAV', '');
define('IMG', 'i/');
define('INC', 'inc/');
}else{
$z = 0;
while($z < $i) {
$current_pos .= '../';
$z++;
}
define('NAV', $current_pos);
define('IMG', $current_pos.'i/');
define('INC', $current_pos.'inc/');
}
Used in the markup it would look something like this:
<a href="<? echo NAV; ?>index.php"><img src="<? echo IMG; ?>spacer.gif" alt="home" /></a>
The site is riddled with these; making it hard to move. I need a better solution without having to wade through all the pages to make changes. Was using getcwd() but it’s not proving reliable. Thanks!
Often it’s convenient to use the
__FILE__constant to find the location of the current file and surrounding directories: