日期:2014-05-17  浏览次数:20439 次

php中获取文件file_a相对于文件file_b的目录

?

?

/**
 * @desc 获取文件file_a相对于文件file_b的目录
 * 
 * @author Jihayang
 * @return string
 */
function getFileRelativePath($file_a, $file_b){
    #$file_a^$file_b 按位或运算(数学中称异或),将字符串转为16进制,相同为0,否则为1,
    #查找$file_a^$file_b里连续等于\x00直到不等于\x00后结束,并返回当前的位置,
    $pos = strspn($file_a^$file_b,"\x00");
    #输出多少个上级相同目录../
    $r_path = str_repeat('../', substr_count($file_a,'/',$pos));
    #输出相对路径
    $r_path .= substr($file_b,strrpos(substr($file_b,0,$pos), '/')+1);
    $r_path = dirname($r_path);
    return  $r_path;
}

$file_a='/a/66/bcdd/44/app/index.php';
$file_b='/a/66/bcdf/goods.php';

echo getFileRelativePath($file_a, $file_b);

?