original_string: public static function getRelativePath($frompath, $topath = "")
      {
          if(empty($topath)) {
              $topath = $frompath;
              $frompath = getcwd();
          }
  
          if($frompath == ".") $frompath = getcwd();
          if($topath == ".") $topath = getcwd();
  
          if($frompath[0] != DIRECTORY_SEPARATOR) $frompath = getcwd().DIRECTORY_SEPARATOR.$frompath;
          if($topath[0] != DIRECTORY_SEPARATOR) $topath = getcwd().DIRECTORY_SEPARATOR.$topath;
  
          $from    = explode(DIRECTORY_SEPARATOR, $frompath); // Folders/File
          $to      = explode(DIRECTORY_SEPARATOR, $topath); // Folders/File
          $relpath = '';
  
          $i = 0;
          // Find how far the path is the same
          while (isset($from[$i]) && isset($to[$i])) {
              if ($from[$i] != $to[$i])
                  break;
              $i++;
          }
          $j = count($from) - 1;
          // Add '..' until the path is the same
          while ($i <= $j) {
              if (!empty($from[$j]))
                  $relpath .= '..' . DIRECTORY_SEPARATOR;
              $j--;
          }
  
          // Go to folder from where it starts differing
          while (isset($to[$i])) {
              if (!empty($to[$i]))
                  $relpath .= $to[$i] . DIRECTORY_SEPARATOR;
              $i++;
          }
  
          // Strip last separator
          return substr($relpath, 0, -1) ?: ".";
      }
code: public static function getRelativePath($frompath, $topath = "")
      {
          if(empty($topath)) {
              $topath = $frompath;
              $frompath = getcwd();
          }
  
          if($frompath == ".") $frompath = getcwd();
          if($topath == ".") $topath = getcwd();
  
          if($frompath[0] != DIRECTORY_SEPARATOR) $frompath = getcwd().DIRECTORY_SEPARATOR.$frompath;
          if($topath[0] != DIRECTORY_SEPARATOR) $topath = getcwd().DIRECTORY_SEPARATOR.$topath;
  
          $from    = explode(DIRECTORY_SEPARATOR, $frompath); // Folders/File
          $to      = explode(DIRECTORY_SEPARATOR, $topath); // Folders/File
          $relpath = '';
  
          $i = 0;
          // Find how far the path is the same
          while (isset($from[$i]) && isset($to[$i])) {
              if ($from[$i] != $to[$i])
                  break;
              $i++;
          }
          $j = count($from) - 1;
          // Add '..' until the path is the same
          while ($i <= $j) {
              if (!empty($from[$j]))
                  $relpath .= '..' . DIRECTORY_SEPARATOR;
              $j--;
          }
  
          // Go to folder from where it starts differing
          while (isset($to[$i])) {
              if (!empty($to[$i]))
                  $relpath .= $to[$i] . DIRECTORY_SEPARATOR;
              $i++;
          }
  
          // Strip last separator
          return substr($relpath, 0, -1) ?: ".";
      }
code_tokens: public static function getRelativePath ( $ frompath , $ topath = "" ) { if ( empty ( $ topath ) ) { $ topath = $ frompath ; $ frompath = getcwd ( ) ; } if ( $ frompath == "." ) $ frompath = getcwd ( ) ; if ( $ topath == "." ) $ topath = getcwd ( ) ; if ( $ frompath [ 0 ] != DIRECTORY_SEPARATOR ) $ frompath = getcwd ( ) . DIRECTORY_SEPARATOR . $ frompath ; if ( $ topath [ 0 ] != DIRECTORY_SEPARATOR ) $ topath = getcwd ( ) . DIRECTORY_SEPARATOR . $ topath ; $ from = explode ( DIRECTORY_SEPARATOR , $ frompath ) ; // Folders/File $ to = explode ( DIRECTORY_SEPARATOR , $ topath ) ; // Folders/File $ relpath = '' ; $ i = 0 ; // Find how far the path is the same while ( isset ( $ from [ $ i ] ) && isset ( $ to [ $ i ] ) ) { if ( $ from [ $ i ] != $ to [ $ i ] ) break ; $ i ++ ; } $ j = count ( $ from ) - 1 ; // Add '..' until the path is the same while ( $ i <= $ j ) { if ( ! empty ( $ from [ $ j ] ) ) $ relpath .= '..' . DIRECTORY_SEPARATOR ; $ j -- ; } // Go to folder from where it starts differing while ( isset ( $ to [ $ i ] ) ) { if ( ! empty ( $ to [ $ i ] ) ) $ relpath .= $ to [ $ i ] . DIRECTORY_SEPARATOR ; $ i ++ ; } // Strip last separator return substr ( $ relpath , 0 , - 1 ) ? : "." ; }
docstring: Get the relative path between 2 folders.
  
  @var string $frompath The path from which the relative path should be computed.
  @var string $topath The target path. If empty or not specified the current working directory will be used.
  @return string The relative path. "$frompath$result" will resolve to $topath
docstring_tokens: Get the relative path between 2 folders .