Windows 案例问题上的 PHP realpath [英] PHP realpath on Windows Case Issue
问题描述
我的 Windows 服务器上有一个符号链接,它是这样制作的:
I have a symlink on my Windows server which was made like this:
F:\>mkdir link-target
F:\>mklink /D link f:\link-target
(注意符号链接目标中的小写f:
)
在 PHP 中我运行这个:
In PHP I run this:
$dir = realpath('f:\link');
var_dump($dir);
$dir = realpath($dir);
var_dump($dir);
输出:
string 'f:\link-target' (length=14)
string 'F:\link-target' (length=14)
注意第二个真实路径上大小写的变化.
Notice the change in case on the second realpath.
这是错误还是有意为之?解决它的最佳方法是什么?
Is this a bug, or intended? And whats the best way to work around it?
它正在打破这样的案例:
It is breaking a case like this:
function check_link($to, $from) {
if (realpath($to) !== realpath($from)) {
...
}
}
用于检查$to
是否存在,并链接到$from
.
Which is used to check $to
exists, and is linked to $from
.
- PHP 5.4
- Windows 7
我需要在 Windows 和 Linux 上保持一致的行为,并且要解决以下问题:
I need consistent behavior on both Windows and Linux, and have the following work around be its pretty nasty:
if (realpath($from) === false) {
} elseif (realpath($to) === false) {
} else {
do {
$to = realpath($to);
} while (realpath($to) !== false && $to !== realpath($to));
do {
$from = realpath($from);
} while (realpath($from) !== false && $from !== realpath($from));
if ($to !== $from) {
...
}
}
编辑 2:
经过进一步调查,我注意到在 Windows 上,符号链接仅跟踪 1 级深度:
On furter investigation I have noticed that on Windows symlinks are only followed 1 level deep:
// F:\>mkdir link-target
// F:\>mklink /D link f:\link-target
// F:\>mklink /D link2 f:\link
$dir = realpath('f:\link2');
var_dump($dir);
$dir = realpath($dir);
var_dump($dir);
$dir = realpath($dir);
var_dump($dir);
// string 'f:\link' (length=7)
// string 'f:\link-target' (length=14)
// string 'F:\link-target' (length=14)
推荐答案
结果
do {
$to = realpath($to);
} while (realpath($to) !== false && $to !== realpath($to));
是唯一的方法.
https://bugs.php.net/bug.php?id=61933
这篇关于Windows 案例问题上的 PHP realpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!