计算文件的相对路径

如:$a = '/a/b/c/d/e.php';
$b='/a/b/12/34/c.php';
计算出$b相当于$a的相对路径

function getRelativePath($a,$b)
{
	
    $arrA = explode('/',$a);
    $arrB = explode('/',$b);
    for ($n = 1,$len = count($arrB);$n<$len;$n++){
    	if($arrA[$n] != $arrB[$n]){
        	break;
        }
        
    }
    if($len - $n > 0){
    	$return = array_fill(1,$len-$n,'..');
    }
    var_dump($return).'<br>';
    $return = array_merge($return,array_slice($arrA,$n));
    return implode('/',$return);
}

  

posted on 2018-10-31 12:36  荣耀王者  阅读(172)  评论(0编辑  收藏  举报

导航