相对 Web 路径与绝对路径
这是一个由 3 部分组成的问题
相对路径是否存在性能或安全风险?
如何将相对路径转换为绝对路径[在用 PHP 编写的大型项目中]?
在 PHP 中使用 include
时我应该给出绝对路径还是相对路径?
this is a 3 part question
Is there any performance or security risk in relative path ?
How can i convert relative path to absolute path [in a large project written in PHP] ?
in PHP when using include
should i give the absolute path or the relative path ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PHP中有两种相对路径。根据文件系统的相对路径以
./
或../
开头,不会影响性能。 PHP 还专门解释诸如what/ever.php
之类的相对路径,并将include_path
考虑在内。这通常(明显)慢,因为必须遍历和搜索目录。安全性这个问题太广泛了,并没有直接发挥作用。如果可能存在恶意包含脚本放置在系统的其他位置和 include_path 内,那么我会考虑实际问题,而不是相对路径。
您应该在项目中使用什么,取决于它是否需要可重新定位。最常见的是使用常量ala
PROJECT_ROOT
来模拟绝对路径。要将文件系统相对路径转换为绝对路径,请使用
realpath()
There are two kinds of relative paths in PHP. Relative paths according to the filesystem start with
./
or../
and do not impact performance. PHP also interprets relative paths likewhat/ever.php
specially itself and takes theinclude_path
into account for those. That's usually (measurably) slower since directories have to be traversed and searched.Security, that's too broadly asked, does not directly play into that. And if there are possibly malicious include scripts placed elsewhere on the system and within the include_path, then I would consider that the actual problem, not the relative paths.
What you should use in your project, depends on if it needs to be relocatable. It's most common to simulate absolute paths using a constant ala
PROJECT_ROOT
.To transform a filesystem-relative path into an absolute, use
realpath()