realpath() 不解析符号链接?
我已经读过有关 realpath() 的内容,但是是否有一个函数可以传递基本目录和文件名,可以在不解析符号链接或检查文件是否实际存在的情况下给出以下结果?或者我是否必须使用修改后的realpath()?
"/var/", "../etc///././/passwd" => "/etc/passwd"
I already read about realpath()
, but is there a function that I can pass a base directory and a filename that would give me the following result without resolving symlinks or checking whether files actually exist? Or do I have to use a modified realpath()
?
"/var/", "../etc///././/passwd" => "/etc/passwd"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个 normalize_path() 函数:
如果给定路径是相对路径,则该函数首先在其前面添加当前工作目录。
然后处理特殊路径组件,如
..
、.
或空组件,并返回结果。对于
..
,如果有最后一个组件,则将其删除(/..
将仅返回/
)。对于
.
或空组件(双/
),这将被跳过。该函数确保不会返回空路径(而是返回
/
)。Here is a normalize_path() function:
If the given path is relative, the function starts by prepending the current working directory to it.
Then the special path components like
..
,.
or empty components are treated, and the result is returned.For
..
, the last component is removed if there is one (/..
will just return/
).For
.
or empty components (double/
), this is just skipped.The function ensures to not return empty an path (
/
is returned instead).(在我写这篇文章时,这个问题被标记为 PHP。)
无论如何,这里是一个正则表达式版本:
(The question was tagged PHP at the time I wrote this.)
Anyway, here is a regex version:
我使用 Hardex 的 解决方案:
示例:
输出:
Note:
char*
,它必须具有包含规范化路径所需的内存/容量。I use Hardex's solution:
Example:
Output:
Note:
char*
which must have the required memory/capacity to contain the normalized path.