diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index a8701533cbd38..2188c96bf2600 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -456,15 +456,18 @@ public function makePathRelative($endPath, $startPath) } $stripDriveLetter = function ($path) { - if (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) { - return substr($path, 2); - } - - return $path; + return (\strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) + ? [substr($path, 2), $path[0]] + : [$path, null]; }; - $endPath = $stripDriveLetter($endPath); - $startPath = $stripDriveLetter($startPath); + list($endPath, $endDriveLetter) = $stripDriveLetter($endPath); + list($startPath, $startDriveLetter) = $stripDriveLetter($startPath); + + if ($endDriveLetter && $startDriveLetter && $endDriveLetter <> $startDriveLetter) { + // Different drives + return $endDriveLetter.':'.$endPath; + } // Split the paths into arrays $startPathArr = explode('/', trim($startPath, '/')); diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index e9e7784a3af40..a3d8d98dfe870 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1115,6 +1115,7 @@ public function providePathsForMakePathRelative() ['C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'], ['C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'], ['C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'], + ['D:/aa/bb', 'C:/aa', 'D:/aa/bb'], ]; if ('\\' === \DIRECTORY_SEPARATOR) {