require_once 中包含路径解析如何工作?
我正在用 PHP 编写一个 Web 应用程序,当时遇到了一个奇怪的情况。为了说明我的问题,请考虑以下结构的 Web 应用程序:
/
index.php
f1/
f1.php
f2/
f2.php
这些文件的内容:
index.php:
<?php require_once("f1/f1.php"); ?>
f1.php:
<?php require_once("../f2/f2.php"); ?>
f2.php:
现在空白 当我尝试在浏览器中打开 index.php 时,出现此错误:
Warning: require_once(../f2/f2.php) [function.require-once]:
failed to open stream: No such file or directory in /var/www/reqtest/f1/f1.php on line 2
Fatal error: require_once() [function.require]:
Failed opening required '../f2/f2.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/reqtest/f1/f1.php on line 2
是否存在我缺少什么明显的东西吗? PHP 中的包含路径如何工作?
在我问这个问题之前,我尝试尝试并找出答案。我设置了另一个测试,如下所示:
/
index.php
f1/
f1.php
f2.php
index.php:
<?php require_once("f1/f1.php"); ?>
f1.php:
<?php require_once("f2.php"); ?>
f2.php:空白
令我惊讶(并且完全困惑)的是,结果很好!
那么,路径解析背后的秘密是什么?
PS我看到了这个问题,但它仍然没有回答我在这里所说的第二种情况。
I was writing an web app in PHP, when I encountered a strange situation. To illustrate my problem, consider a web app of this structure:
/
index.php
f1/
f1.php
f2/
f2.php
Contents of these files:
index.php:
<?php require_once("f1/f1.php"); ?>
f1.php:
<?php require_once("../f2/f2.php"); ?>
f2.php: blank
now when I try to open index.php in my browser I get this error:
Warning: require_once(../f2/f2.php) [function.require-once]:
failed to open stream: No such file or directory in /var/www/reqtest/f1/f1.php on line 2
Fatal error: require_once() [function.require]:
Failed opening required '../f2/f2.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/reqtest/f1/f1.php on line 2
Is there something obvious I'm missing? how do include paths work in PHP?
Before I asked this question, I attempted to experiment and find out. I set up another test, like so:
/
index.php
f1/
f1.php
f2.php
index.php:
<?php require_once("f1/f1.php"); ?>
f1.php:
<?php require_once("f2.php"); ?>
f2.php: blank
To my surprise (and utter confusion), this worked out fine!
So, what is the secret behind the path resolution?
PS I saw this question, but it still does not answer the second case that I've stated here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果包含另一个文件,工作目录仍保留在包含文件所在的位置。
您的示例正在按预期工作。
编辑:第二个示例之所以有效,是因为 . (实际目录)位于您的包含路径中(请参阅错误消息)。
编辑2:
在第二个示例中,您感兴趣的关键点是这一行:
首先它将查找当前工作目录(
/var/www/req_path_test
),但找不到 f2.php。作为后备,它将尝试在您的 include_path (
'.:/usr/share/php:/usr/share/pear'
) 中查找以 '.' 开头的 f2.php (这是相对于实际文件,而不是包括文件)。所以 './f2.php' 可以工作并且 require 不会失败。
If you include another file, the working directory remains where the including file is.
Your examples are working as intended.
Edit: The second example works because . (actual directory) is in your include path (see your error message).
Edit2:
In your second example, the key point of your interest is this line:
At first it will look in the current working dir (
/var/www/req_path_test
), but does not find f2.php.As fallback, it will try to find f2.php in your include_path (
'.:/usr/share/php:/usr/share/pear'
), starting with '.' (which is relative to the actual file, not the including one).So './f2.php' works and the require does not fail.
当您打开index.php时,工作目录将设置为该文件所在的文件夹。并且在包含的f1.php中,该工作目录不会更改。
您可以使用相对于当前包含文件的绝对路径来包含文件,如下所示:
但如果这些文件包含类,最好考虑使用自动加载器。
When you open index.php, working dir is set to the folder this file resides in. And inside insluded f1.php this working dir does not change.
You can include files by using their absolute paths, relative to the current included file like this:
But better consider using an autoloader if these files contain classes.
通常在旧结构中
而不是
应该可以工作。据我所知 php 从初始脚本获取路径
Normaly in you old structure
<?php require_once("f2/f2.php"); ?>
instead of
<?php require_once("../f2/f2.php"); ?>
should work. As far as i know php takes the paths from the initial script
听起来您的服务器已启用 open_basedir 设置在 PHP 配置中。这使得无法在目录结构中包含(和打开)文件夹中的文件(即,您不能使用 ../ 在文件夹结构中向上移动)。
It sounds like your server has the open_basedir setting enabled in the PHP configuration. This makes it impossible to include (and open) files in folders above your in the directory structur (i.e., you can't use ../ to go up in the folder structure).
来自 PHP 文档 PHP include
如果未给出文件路径,则即 require_once("f2.php");
第一。 第二次检查 include_path
。 第三步检查调用脚本自己的目录
。最后检查当前工作目录
如果未找到文件,则 PHP 会在文件 include & 上抛出警告。需要致命错误
如果您包含/需要以 .或 .. 或 ./ 那么 PHP 的解析器将在父目录中查找,即当前工作目录,即 require_once("../f2/f2.php"),php 将在根目录中检查调用脚本 index.php位于该目录中。
您的代码不起作用,因为 -
因此你会得到致命错误
您的代码可以工作,因为 -
因此您的代码可以正常工作!
From the PHP Docs PHP include
If the file path is not given then i.e require_once("f2.php");
1st. The include_path is checked
2nd. The calling scripts own directory is checked
3rd. Finally the current working directory is checked
If file not found then PHP throws warning on file include & fatal error on require
If you include/require your file beginning with . or .. or ./ then PHP's parser will look in the parent directory which is the current working directory i.e require_once("../f2/f2.php"), php will check at the root directory as the calling script index.php is in that directory.
You code does not work because -
Thus you get the Fatal Error
Your code works because -
Thus Your Code Works Fine!