Tôi biết đây là một chủ đề cũ, nhưng nó thực sự hữu ích.
Tôi gặp sự cố Phar::interceptFileFuncs kỳ lạ khi tôi triển khai đường dẫn tương đối trong phpctags, realpath()
thực sự thực sự lỗi trong phar.
Cảm ơn chủ đề này cung cấp cho tôi một số đèn, ở đây đi kèm với việc triển khai của tôi dựa trên việc thực hiện christian từ chủ đề này và comments này.
Hy vọng nó phù hợp với bạn.
function relativePath($from, $to)
{
$fromPath = absolutePath($from);
$toPath = absolutePath($to);
$fromPathParts = explode(DIRECTORY_SEPARATOR, rtrim($fromPath, DIRECTORY_SEPARATOR));
$toPathParts = explode(DIRECTORY_SEPARATOR, rtrim($toPath, DIRECTORY_SEPARATOR));
while(count($fromPathParts) && count($toPathParts) && ($fromPathParts[0] == $toPathParts[0]))
{
array_shift($fromPathParts);
array_shift($toPathParts);
}
return str_pad("", count($fromPathParts)*3, '..'.DIRECTORY_SEPARATOR).implode(DIRECTORY_SEPARATOR, $toPathParts);
}
function absolutePath($path)
{
$isEmptyPath = (strlen($path) == 0);
$isRelativePath = ($path{0} != '/');
$isWindowsPath = !(strpos($path, ':') === false);
if (($isEmptyPath || $isRelativePath) && !$isWindowsPath)
$path= getcwd().DIRECTORY_SEPARATOR.$path;
// resolve path parts (single dot, double dot and double delimiters)
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$pathParts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutePathParts = array();
foreach ($pathParts as $part) {
if ($part == '.')
continue;
if ($part == '..') {
array_pop($absolutePathParts);
} else {
$absolutePathParts[] = $part;
}
}
$path = implode(DIRECTORY_SEPARATOR, $absolutePathParts);
// resolve any symlinks
if (file_exists($path) && linkinfo($path)>0)
$path = readlink($path);
// put initial separator that could have been lost
$path= (!$isWindowsPath ? '/'.$path : $path);
return $path;
}
Nguồn
2013-12-05 08:19:14
xin vui lòng truy cập [bugs.php.net] (http://bugs.php.net "Trình gỡ lỗi của PHP") và xem các lỗi bạn gặp phải đã được liệt kê chưa. Nếu không, vui lòng gửi báo cáo lỗi để chúng được khắc phục. – Gordon
Chúng được ghi lại, tuy nhiên, ngay cả khi chúng không phải là bản vá không thể giúp các phiên bản PHP ("ổn định") sớm hơn ... tôi cần phải làm việc trên một cái gì đó thực sự hoạt động. – Christian
chăm sóc để chia sẻ liên kết đến báo cáo lỗi? –