Return to Snippet

Revision: 43627
at March 28, 2011 11:34 by jHulbert


Initial Code
function rel_path($root=NULL) {
	// Get script name if exists
	$cur_file = $_SERVER["SCRIPT_NAME"];
    $cur_file = explode('/', $cur_file);
    $cur_file = $cur_file[count($cur_file) - 1];
	// Remove script name
	$tmp = str_replace($cur_file, "", $tmp);
	// Split at specified root folder
	if($root != NULL) {
		$tmp = split($root, $_SERVER['REQUEST_URI']);
		$tmp = $tmp[1];
	}
	// Make windows-friendly
    $tmp = str_replace('\\', '/', $tmp);
	// Create path array
    $tmp = explode('/', $tmp);

    $rel_path = NULL;        
    for ($i = 0; $i < count($tmp); $i++) {
        if ($tmp[$i] != '') {
            $rel_path .= '../';
		}
    }

    if ($rel_path != NULL) { 
		$rel_path = substr($rel_path, 0, -1); 
	} else { 
		$rel_path = '.'; 
	}
	
    return $rel_path;
}

Initial URL

                                

Initial Description

                                

Initial Title
Get relative path to root folder

Initial Tags
php

Initial Language
PHP