没有符号链接解析的绝对路径/将用户保留在主目录中
PHP 中有没有一种方法可以在不实际解析符号链接的情况下确定给定相对路径的绝对路径?类似于 realpath 函数,但没有符号链接解析。
或者,是否有一些简单的方法来检查用户(使用我的浏览 PHP 脚本查看文件)是否没有意外地走出 Apache 虚拟主机的主目录? (或者不允许他在路径中使用令人讨厌的 . 和 ..)
谢谢!
Is there a way in PHP how to determine an absolute path given a relative path without actually resolving symlinks? Something like the realpath
function but without symlink resolution.
Or alternatively, is there some easy way to check whether user (who uses my browsing PHP script to view files) did not accidentally step out of Apache virtual host's home directory? (or disallow him to use the nasty . and .. in paths)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道 PHP 本机解决方案,但这里有一个很好的绝对路径实现 PHP 文档中的注释:
(针对格式和拼写错误进行了编辑)
I don't know PHP native solution for this, but here's one nice absolute path implementation from a comment in the PHP docs:
(edited for formatting & typos)
您可以使用 open_basedir 设置 ini php.ini 或虚拟主机声明(如 php_admin_value 指令)。
You can use open_basedir setting ini php.ini or in virtual host declaration (as php_admin_value directive).
这是我模仿原始
realpath()
的方法,它还:c:
中所示。注意:这种方法使用正则表达式,众所周知,正则表达式的 CPU 资源消耗很大,所以我喜欢使用 http://www.php.net/manual/en/function.realpath.php#84012。
Here is my approach mimicking the original
realpath()
which also:c:
.NB: This approach uses regular expressions which are known to be CPU expensive, so I like the method using arrays from http://www.php.net/manual/en/function.realpath.php#84012.
@Karolis 提供了可接受的答案,但这并不完全符合我所读到的问题。例如,以下脚本:
执行时,将生成:
... 这不是绝对路径。在他的辩护中,Karolis 在他的回答中提到了这一点,他说“...在位置 0 处不包含(反)斜杠”
正确的答案是:
对 @Karolis 答案的以下轻微修改是:
这仍然无法解决符号链接,但将解析传递值的“相对”部分。
@Karolis provided the accepted answer, but that does not quite do what I read the question to be. For example, the following script:
when executed, will produce:
... which is not an absolute path. In his defense, Karolis alluded to this in his answer, saying "... contain no (back)slash at position 0"
The correct answer would be:
The following slight modification to @Karolis' answer would be:
This still does not resolve symbolic links, but will resolve the "relative" portion of the passed value.