public function getRelativePath($from, $to)
{
// Compatibility fixes for Windows paths
$from = is_dir($from) ? rtrim($from, '\\/') . '/' : $from;
$to = is_dir($to) ? rtrim($to, '\\/') . '/' : $to;
$from = str_replace('\\', '/', $from);
$to = str_replace('\\', '/', $to);
$from = explode('/', $from);
$to = explode('/', $to);
$relPath = $to;
foreach ($from as $depth => $dir) {
if ($dir !== $to[$depth]) {
// Number of remaining directories
$remaining = count($from) - $depth;
if ($remaining > 1) {
// Add traversals up to first matching directory
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
}
$relPath[0] = './' . $relPath[0];
}
array_shift($relPath);
}
return implode('/', $relPath);
}