This function get the relative path , and it can worked in the windows operation system correctly , but in the other operation system like linux or others , it may had some problems , in addition , I don't test it in other system at all .
/** * get the relative path , only suit windows path * @param string $path_from the path will begin from * @param string $path_to the path will direct to */ function relative_path($path_from,$path_to){ if (FALSE === ($path_from = realpath($path_from)) or FALSE === ($path_to = realpath($path_to))){ //can't find the file path , so the file doesn't exist , return false return FALSE; } $path_from = is_file($path_from) ? dirname($path_from) : $path_from; $path_to = is_file($path_to) ? dirname($path_to) : $path_to; $path_from_array = explode('//', $path_from); $path_to_array = explode('//', $path_to); if ($path_from_array[0] == $path_to_array[0]){ $relative_path = ''; $count_path_from_array = count($path_from_array); $count_path_to_array = count($path_to_array); $set_slice = false; $slice_begin = NULL; for ($i = 0 ; $i < $count_path_from_array ; $i++){ if (! isset($path_to_array) or $path_to_array[$i] != $path_from_array[$i]){ $relative_path .= '..//'; if (!$set_slice){ $slice_begin = $i; $set_slice = true; } } } if ($slice_begin <= $count_path_to_array){ $relative_path .= implode('//', array_slice($path_to_array,$slice_begin,$count_path_to_array-$slice_begin)); } return $relative_path; }else { //the two files are in diferent disc for example : 'D:' and 'E:' will return false return FALSE; } } //example: $path_from = 'index.php'; $path_to = '../c_c++/c'; if ($re_path = relative_path($path_from, $path_to)){ echo $re_path; //result just like: '../c_c++/c'; } //end file