算出两个文件的相对路径

function abpath($a, $b) {

    $ainfo = parse_url($a);

    $binfo = parse_url($b);
    $apath = ltrim($ainfo['path'], '/');
    $bpath = ltrim($binfo['path'], '/');

    $arr = explode('/',$apath);
    $brr = explode('/',$bpath);
    
    foreach ($arr as $key=>$value) {
        if($value==$brr[$key]) {
            $brr[$key] = '..';
        }
    }
    $cha = implode('/',$brr);
    return $cha;
}

计算文件$b相对于文件$a的路径,如 

$a = ’/a/b/c/d/e.php’; $b = ’/a/b/12/34/c.php’;  

结果

../../12/34/c.php

你可能感兴趣的:(PHP,相对路径)