替换 PHP 的 realpath()
显然,realpath
有很多错误。在 PHP 5.3.1 中,它会导致随机崩溃。 在 5.3.0 及更低版本中,realpath
随机失败并返回 false(当然对于相同的字符串),而且它总是在 realpath
上失败 - 同一个字符串两次/多次(当然,第一次就可以了)。
而且,它在早期的 PHP 版本中存在如此多的错误,以至于完全无法使用。嗯...已经是了,因为它不一致。
无论如何,我有什么选择?也许我自己重写一下?这是可取的吗?
Apparently, realpath
is very buggy. In PHP 5.3.1, it causes random crashes.
In 5.3.0 and less, realpath
randomly fails and returns false (for the same string of course), plus it always fails on realpath
-ing the same string twice/more (and of course, it works the first time).
Also, it is so buggy in earlier PHP versions, that it is completely unusable. Well...it already is, since it's not consistent.
Anyhow, what options do I have? Maybe rewrite it by myself? Is this advisable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
感谢 Sven Arduwie 的代码(Pekka 指出)和一些修改,我构建了一个(希望)更好的实现:
注意:与 PHP 的
realpath
不同,此函数在出错时不会返回 false;它返回一条尽可能解决这些怪癖的路径。注2:显然有些人无法正确阅读。 Truepath() 不适用于包括 UNC 和 URL 在内的网络资源。 它仅适用于本地文件系统。
Thanks to Sven Arduwie's code (pointed out by Pekka) and some modification, I've built a (hopefully) better implementation:
NB: Unlike PHP's
realpath
, this function does not return false on error; it returns a path which is as far as it could to resolving these quirks.Note 2: Apparently some people can't read properly. Truepath() does not work on network resources including UNC and URLs. It works for the local file system only.
这是修改后的代码,也支持 UNC 路径
here is the modified code that supports UNC paths as well
我知道这是一个旧线程,但它确实很有帮助。
当我实现相对路径时,我遇到了一个奇怪的 Phar::interceptFileFuncs 问题在 phpctags 中,
realpath()
在 phar 内部确实有 bug 。感谢这个线程给了我一些启发,这里是我的实现,基于 christian 从这个线程和这个 评论。
希望它对你有用。
I know this is an old thread, but it is really helpful.
I meet a weird Phar::interceptFileFuncs issue when I implemented relative path in phpctags, the
realpath()
is really really buggy inside phar.Thanks this thread give me some lights, here comes with my implementation based on christian's implemenation from this thread and this comments.
Hope it works for you.
对于那些 Zend 用户来说,这个答案可能会帮助你,就像它对我一样:
For those Zend users out there, THIS answer may help you, as it did me:
我从未听说过 realpath() 存在如此大的问题(我一直认为它只是接口一些底层操作系统功能 - 会对某些链接感兴趣),但是 用户贡献的注释到手册页有许多替代实现。 这里看起来不错。
当然,不能保证这些实现能够解决所有跨平台的怪癖和问题,因此您必须进行彻底的测试,看看它是否适合您的需求。
据我所知,它们都没有返回规范化路径,它们只解析相对路径。如果您需要它,我不确定您是否可以绕过 realpath() (除非执行一个(依赖于系统的)控制台命令来为您提供完整路径。)
I have never heard of such massive problems with
realpath()
(I always thought that it just interfaces some underlying OS functionality - would be interested in some links), but the User Contributed Notes to the manual page have a number of alternative implementations. Here is one that looks okay.Of course, it's not guaranteed these implementations take care of all cross-platform quirks and issues, so you'd have to do thorough testing to see whether it suits your needs.
As far as I can see though, none of them returns a canonicalized path, they only resolve relative paths. If you need that, I'm not sure whether you can get around
realpath()
(except perhaps executing a (system-dependent) console command that gives you the full path.)在 Windows 7 上,代码运行良好。在 Linux 上,存在一个问题,即生成的路径以(在我的情况下)home/xxx 开头,而它应该以 /home/xxx 开头...即表示根文件夹的初始 / 丢失。
问题不在于这个函数,而在于 Linux 中 getcwd 返回的内容。
On Windows 7, the code works fine. On Linux, there is a problem in that the path generated starts with (in my case) home/xxx when it should start with /home/xxx ... ie the initial /, indicating the root folder, is missing.
The problem is not so much with this function, but with what getcwd returns in Linux.