Php source code calculates the relative paths of two files,
<? Php // calculate the relative path of the two files, that is, the relative path of path2 to $ path1 // http://www.manongjc.com/article/1342.htmlfunction get_relative_path ($ path1, $ path2) {$ arr1 = explode (DIRECTORY_SEPARATOR, dirname ($ path1); $ arr2 = explode (DIRECTORY_SEPARATOR, dirname ($ path2); $ length = count ($ arr2); for ($ I = 0; $ I <$ length; $ I ++) {if ($ arr1 [$ I]! = $ Arr2 [$ I]) {// Path Matching break;} if ($ I = 0) {// different return 'in the root directory is not in the same directory';} else if ($ I! = 0 & $ I <$ length) {// $ separator = array_fill (0, ($ length-$ I ),'.. '); return (implode (DIRECTORY_SEPARATOR, array_merge ($ separator, array_slice ($ arr1, $ I);} else if ($ I = $ length) {// The path is exactly the same http://www.manongjc.com/article/1343.html return '. ';}}$ path1 = 'f: \ Apache \ www \ temp \ php_demo \ demo. md '; // Reference Path path1 $ path2 = 'f: \ Apache \ www \ temp \ php_demo \ demo. md '; $ path3 = 'f: \ Apache \ www \ temp \ sd \ ss. d \ Sdsfd '; $ path4 = 'G: \ ffout'; var_dump (get_relative_path ($ path1, $ path2); var_dump (get_relative_path ($ path1, $ path3 )); var_dump (get_relative_path ($ path1, $ path4);?>