function relativepath($to){
$a=explode("/",$_SERVER["PHP_SELF"] );
$index= array_search("$to",$a);
$str="";
for ($i = 0; $i < count($a)-$index-2; $i++) {
$str.= "../";
}
return $str;
}
Here is the best solution i made about that, you just need to specify at which level you want to stop, but the problem is that you have to use this folder name one time.