Home  >  Article  >  php教程  >  php求两个文件相对路径

php求两个文件相对路径

WBOY
WBOYOriginal
2016-06-13 10:45:11984browse

//用php求两个文件的相对路径
function compara_path($path_a, $path_b) {
 //切割路径.
 $array_a =explode('/', $path_a);
 $array_b =explode('/', $path_b);
 //从数组中删除最后的文件,剩下的全为文件夹名称.
 $file_a =array_pop($array_a); //array_pop() 弹出并返回 array 数组的最后一个单元
 $file_b =array_pop($array_b);
 //子目录个数.
 $a_len =count($array_a);
 $b_len =count($array_b);
 //循环求出第几个目录不同.
 for ( $i =0; $i   if ($array_a[$i] != $array_b[$i] ) {
      break;
  }
 }
 //求出相对路径.
 $com_path ="";
 for ( $j =0; $j      $com_path .='../';
 }
 for ( $i; $i      $com_path .=$array_b[$i] . '/';
 }
 $com_path .=$file_b;
 return $com_path;
}
$path_a = "a/b/c/d/e/f.php";
$path_b = "a/b/z/x/y.php";
echo compara_path($path_a, $path_b);

摘自:小囧的博客

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn