If it\'s Path_To_DocumentRoot/a/b/c.php
,should always be /a/b
.
I use this:
dirname($_SERVER[\"PHP_SELF\"])
<
Here's a general purpose function to get the relative path between two paths.
/**
* Return relative path between two sources
* @param $from
* @param $to
* @param string $separator
* @return string
*/
function relativePath($from, $to, $separator = DIRECTORY_SEPARATOR)
{
$from = str_replace(array('/', '\\'), $separator, $from);
$to = str_replace(array('/', '\\'), $separator, $to);
$arFrom = explode($separator, rtrim($from, $separator));
$arTo = explode($separator, rtrim($to, $separator));
while(count($arFrom) && count($arTo) && ($arFrom[0] == $arTo[0]))
{
array_shift($arFrom);
array_shift($arTo);
}
return str_pad("", count($arFrom) * 3, '..'.$separator).implode($separator, $arTo);
}
Examples
relativePath('c:\temp\foo\bar', 'c:\temp'); // Result: ../../
relativePath('c:\temp\foo\bar', 'c:\\'); // Result: ../../../
relativePath('c:\temp\foo\bar', 'c:\temp\foo\bar\lala'); // Result: lala