【发布时间】:2021-09-20 06:12:33
【问题描述】:
当我遇到一个奇怪的情况时,我正在用 PHP 编写一个 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 我看到了this question,但它仍然没有回答我在这里所说的第二种情况。
【问题讨论】:
-
我已经绕过了这个问题(使用目录名)。我想知道的是为什么第二种情况不会失败。是错误还是功能?
-
编辑了我的答案以涵盖第二个示例。
-
我找不到记录从 f1.php 成功调用 require_once('f2.php') 的手册页。文档说当没有提供路径信息时会忽略 include_path(无论如何,从 include_path 中删除 '.' 无效)并且 getcwd() 表明工作目录在包含链周围都是相同的。说真的,它看起来像是一个未记录的功能。
标签: php include include-path require-once