echo RelativePath("/hoge2/hoge3/hoge1" , "/hoge2/hoge3/mydir" );

function RelativePath( $basePath , $targetPath) {
$basePath = ereg_replace("/$","",$basePath);
$targetPath = ereg_replace("/$","",$targetPath);
$basePath = ereg_replace("^/","",$basePath);
$targetPath = ereg_replace("^/","",$targetPath);
$base = explode("/",$basePath);
$target = explode("/",$targetPath);
$targetCount = count($target);
$baseCount = count($base);
for ($i=0;$i<$targetCount and $i<$baseCount;$i++){
if ( $base[$i] == $target[$i] ){
unset( $target[$i]);
unset( $base[$i]);
}
}
if (count($base) ==0)
$path ="./";
else
$path = str_repeat("../", count( $base ));
$path.= implode("/",$target);;
return $path;
}