路径混乱:相对/绝对/相对于 DOCUMENT_ROOT?

发布于 2024-10-14 18:34:37 字数 837 浏览 3 评论 0原文

我刚刚开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2024-10-21 18:34:37

PHP 手册

根据文件包含文件
给出的路径,或者,如果没有给出,则
指定include_path。如果文件
在 include_path 中找不到,
include() 最终会检查
调用脚本自己的目录和
当前工作目录之前
失败。

因此,最好的做法是使用 set_include_path() 方法在主文件(在您的例子中为 index.php)中设置包含路径。

使用框架或自定义服务器时,最好通过执行以下操作来指定包含路径,而不留下系统定义的包含路径:

set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);

这样,新的包含路径将添加到现有路径中。

对于您在问题中提出的问题,最好指定我上面提到的包含路径,然后相对地引用您的文件。如果将来您想要移动文件,只需编辑一行即可,而不是在数百个文件中查找和替换。

例如:

index.php:

set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
require('folder1/file.php');

folder1/file.php:

require('folder2/file.php');

folder2/file.php:

require('folder3/file.php');

等等..

The PHP manual says:

Files are included based on the file
path given or, if none is given, the
include_path specified. If the file
isn't found in the include_path,
include() will finally check in the
calling script's own directory and the
current working directory before
failing.

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:

set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);

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:

set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
require('folder1/file.php');

folder1/file.php:

require('folder2/file.php');

folder2/file.php:

require('folder3/file.php');

and so on..

只有一腔孤勇 2024-10-21 18:34:37

只是因为它是很好的练习。并非所有使用folder2/file.php 的脚本都会从根目录调用。

假设您有

index.php
folder1\file.php
folder2\file.php
folder3\use-folder2.php

并且尝试从 use-folder2.php 中包含来自folder2的file.php,那么使用您的想法,它将查找该文件的路径是:

folder3\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

index.php
folder1\file.php
folder2\file.php
folder3\use-folder2.php

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 :

folder3\folder2\file.php

which doesn't exists.

That's why they are making you start your path from the root of your web site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文