路径混乱:相对/绝对/相对于 DOCUMENT_ROOT?
我刚刚开始学习 PHP,并且阅读了几个几乎类似问题的答案,但这没有帮助。
我有几个文件:
index.php
folder1\file.php
folder2\file.php
所以 index.php
包含 folder1\file.php
并且这非常简单。然后 folder1\file.php
包含 folder2\file.php
。我正在读的书说要做到这一点,我必须将其写在 folder1\file.php
中:
require $_SERVER['DOCUMENT_ROOT'] . '/' . 'folder2/file.php';
那么基本上他们为什么不这样做呢?
require 'folder2/file.php';
据我了解,所有路径始终相对于初始脚本的位置,并且它在我的计算机上运行良好。
我仍然不明白为什么不使用 .\folder\file.php
,如果我将目录移得更深或将整个网站复制到另一台计算机,它会保持工作状态。
根据手册:如果定义了路径 - 无论是绝对路径(在 Windows 上以驱动器号或 \
开头,在 Unix/Linux 系统上以 /
开头)还是相对于当前目录(以 .
或 ..
开头) - 包含路径将被完全忽略。例如,如果文件名以 ../
开头,解析器将在父目录中查找。
I have just started learning PHP and I have read several answers to pretty much similar question, but it didn't help.
I have several files:
index.php
folder1\file.php
folder2\file.php
So index.php
includes folder1\file.php
and it is really easy. Then folder1\file.php
includes folder2\file.php
. The book I am reading says to do this I have to write this inside folder1\file.php
:
require $_SERVER['DOCUMENT_ROOT'] . '/' . 'folder2/file.php';
So basically why didn't they do this?
require 'folder2/file.php';
As far as I understood, all the paths are always relative to the initial script's location and it works just fine on my machine.
I still don't get why not use .\folder\file.php
which would keep it working if I move my directory deeper or copy the whole website to another machine.
According to the manual: If a path is defined — whether absolute (starting with a drive letter or \
on Windows, or /
on Unix/Linux systems) or relative to the current directory (starting with .
or ..
) — the include path will be ignored altogether. For example, if a filename begins with ../
, the parser will look in the parent directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 手册说:
因此,最好的做法是使用 set_include_path() 方法在主文件(在您的例子中为 index.php)中设置包含路径。
使用框架或自定义服务器时,最好通过执行以下操作来指定包含路径,而不留下系统定义的包含路径:
这样,新的包含路径将添加到现有路径中。
对于您在问题中提出的问题,最好指定我上面提到的包含路径,然后相对地引用您的文件。如果将来您想要移动文件,只需编辑一行即可,而不是在数百个文件中查找和替换。
例如:
index.php:
folder1/file.php:
folder2/file.php:
等等..
The PHP manual says:
So it's always a best practice to set the include path in your master file (in your case, index.php) using the set_include_path() method.
When working with frameworks or customised servers, it is good to specify your include path without leaving behind the system defined include paths by doing the following:
That way, your new include path will be added to the existing paths.
For what you have asked in the question, it is always better to specify the include path as I mentioned above and then refer your files relatively. If in case in future you want to move your files, editing just one line will do instead of finding and replacing in hundreds of files.
Example:
index.php:
folder1/file.php:
folder2/file.php:
and so on..
只是因为它是很好的练习。并非所有使用folder2/file.php 的脚本都会从根目录调用。
假设您有
并且尝试从 use-folder2.php 中包含来自folder2的file.php,那么使用您的想法,它将查找该文件的路径是:
它不存在。
这就是为什么他们让您从网站的根部开始您的道路。
Simply because it good practice. Not all you scripts using folder2/file.php are going to be called from the root directory.
Say if you have
and you try to include file.php from folder2 from use-folder2.php then using your idea the path it will look for the file would be :
which doesn't exists.
That's why they are making you start your path from the root of your web site.