php - Better way to handle relative paths -
i inherited sites previous developer used spaghetti code below handle relative paths nav, image, , 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 markup this:
<a href="<? echo nav; ?>index.php"><img src="<? echo img; ?>spacer.gif" alt="home" /></a>
the site riddled these; making hard move. need better solution without having wade through pages make changes. using getcwd() it's not proving reliable. thanks!
often it's convenient use __file__
constant find location of current file , surrounding directories:
define('app_path', dirname(__file__) . "/"); $img = app_path . "img/"; $js = app_path . "js/"; // etc // includes... require_once(app_path . "include/file.php"); require_once(app_path . "../upperleveldir/file2.php");
Comments
Post a Comment