These two functions are great for CMS engines that work with files & folders + data in a database.
This first one calculates the path back to the root with a given file and root. It returns '../../../' depending on the depth of the tree.
Version 1
PHP Code:
function pathToRoot($root,$file)
{
/* Function: pathToRoot() created by `s-p-n` For: `WysGui.com`
pathToRoot calculates and returns the path from a specified file back to the specified root.
Example:
pathToRoot($_SERVER['HTTP_HOST'],$_SERVER['REQUEST_URI'])
(returns the path from a remote file back to the host)
*/
$r = count(explode('/',$root));$f = count(explode('/',$file));
for($i=1;$i<($f-$r);$i++)
{
$str .= '../';
}
return $str;
}
I personally use this function on every page in the header.php file.
I call it $_root because it helps me coordinate through HTML like I do in flash.
So I call this var in header.php:
PHP Code:
$_root = pathToRoot($_SERVER['HTTP_HOST'],$_SERVER['REQUEST_URI']);
And on my Nav bar all of the links are $_root.$url. If there is no root, no problem. When a root comes up, again, no problem. (as long as the nav links are relative, of course ;) 
--------------------------------------------------------------
This second function is a fix for some glitches in $_SERVER['REQUEST_URI'] that shows query strings, seizes to show the index as default when the user is visiting a folder. This function returns "path/to/file.ext".
Expanded Version:
PHP Code:
function wysgui_uri($index)
{
/* Function: wysgui_uri() created by `s-p-n` For: `WysGui.com`
$index is optional, default is 'index.php'
Finds this URL and strips all the crap out.
Opens door for smooth coordination between files & MySQL data
If no page is found, it must be root/folder/index.php
(wysgui.com -> index.php, wysgui.com/folder -> folder/index.php)
Example, might be blank for 'wysgui.com/', but we know it's index.php.
*/
$cleanURL = preg_replace('/[?]|'.$_SERVER['QUERY_STRING'].'/','',substr($_SERVER['REQUEST_URI'],1));
$cond = strspn('.',$cleanURL);
$t = $cleanURL;
$f = strlen($index)>0?$index:'index.php';
if(!$cond)
{
$t.=$f;
}
return $t;
}
Collapsed Version:
PHP Code:
function wysgui_uri($index)
{
/* Function: wysgui_uri() created by `s-p-n` For: `WysGui.com`
$index is optional, default is 'index.php'
Finds this URL and strips all the crap out.
Opens door for smooth coordination between files & MySQL data
If no page is found, it must be root/folder/index.php
(wysgui.com -> index.php, wysgui.com/folder -> folder/index.php)
Purpose: $_SERVER['REQUEST_URI'] might be blank for 'wysgui.com/', but we know it's index.php.
It also might contain query strings and junk we don't want.
*/
$cleanURL = preg_replace('/[?]|'.$_SERVER['QUERY_STRING'].'/','',substr($_SERVER['REQUEST_URI'],1));
return strspn('.',$cleanURL)?$cleanURL:$cleanURL.(strlen($index)>0?$index:'index.php');
}
I hope these functions are of use to people ;)
The point for both these functions is destroyed when you decide not to use relative paths. However, if you are going to make a CMS that stores all of the pages with relative paths, (which allows the whole site to be moved from folder-to-folder or across domains), you can do as I do using this system originally developed for http://www.wysgui.com. Nav links are never messed up, and the pageData in the database will have stored the relative page paths; [ which links up exactly with wysgui_uri() ].
If there's a function like either of these default with PHP that'd be cool but I haven't seen one yet, so enjoy.