php 计算两个文件之间的相对路径方法
例如:
文件A 的路径是 /home/web/lib/img/cache.php
文件B的路径是 /home/web/api/img/show.php
那么,文件A相对于文件B的路径是 ../../lib/img/cache.php,即文件B 访问 文件A的相对路径。
function getRelativePath
0){
$prefix = array_fill(0, count($arr2)-$depth-1, '..');
}else{
$prefix = array('.');
}
$tmp = array_merge($prefix, array_slice($arr1, $depth));
$relativePath = implode('/', $tmp);
return $relativePath;
}
?>demo
'; // ./web/lib/img/cache.php $path1 = '/home/web/lib/img/cache.php'; $path2 = '/home/web/api/show.php'; echo getRelativePath($path1, $path2).'
'; // ../lib/img/cache.php $path1 = '/home/web/lib/img/cache.php'; $path2 = '/home/web/api/img/show.php'; echo getRelativePath($path1, $path2).'
'; // ../../lib/img/cache.php $path1 = '/home/web/lib/img/cache.php'; $path2 = '/xhome/web/show.php'; echo getRelativePath($path1, $path2).'
'; // ../../home/web/lib/img/cache.php ?>
本文讲解了通过php 计算两个文件之间的相对路径方法 ,更多相关内容请关注php中文网。
相关推荐:











