//Use php to find the relative paths of two files
function compara_path($path_a, $path_b) {
//Cutting path.
$array_a =explode('/', $path_a);
$array_b =explode('/', $path_b);
//Delete the last file from the array, and the remaining files are all folder names.
$file_a =array_pop($array_a); //array_pop() pops and returns the last element of the array
$file_b =array_pop($array_b);
//Number of subdirectories.
$a_len =count($array_a);
$b_len =count($array_b);
//Loop to find out which directories are different.
for ( $i =0; $i < $a_len; $i++ ) {
if ($array_a[$i] != $array_b[$i] ) {
break;
}
}
//Find the relative path.
$com_path="";
for ( $j =0; $j < $a_len - $i; $j++ ) {
$com_path .='../';
}
for ( $i; $i< $b_len; $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);
Excerpted from: Xiao囧’s blog