为什么这个 PHP 相关包含失败?

发布于 11-14 19:31 字数 1382 浏览 2 评论 0原文

disc@puff:~/php$ ls 
a.php  data  include 

disc@puff:~/php$ tree 
. 
├── a.php 
├── data 
│   └── d.php 
└── include 
    ├── b.php 
    └── c.php 
2 directories, 4 files 

disc@puff:~/php$ cat a.php 
a.php is including include/b.php ... 
<?php include "include/b.php" ?> 

disc@puff:~/php$ cat include/b.php 
b.php is including c.php and ../data/d.php ... 
<?php include "c.php" ?> 
<?php include "../data/d.php" ?> 

disc@puff:~/php$ cat include/c.php 
c.php 

disc@puff:~/php$ cat data/d.php 
d.php 

disc@puff:~/php$ php a.php 
a.php is including include/b.php ... 
b.php is including c.php and ../data/d.php ... 
c.php 
PHP Warning:  include(../data/d.php): failed to open stream: No 
such file or directory in /home/disc/php/include/b.php on line 3 
PHP Warning:  include(): Failed opening '../data/d.php' for 
inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/ 
disc/php/include/b.php on line 3 

disc@puff:~/php$ 

为什么 include "c.php" 成功但 include "../data/d.php" 失败?

http://www.php.net/manual/en/function.include.php 提到:“如果一条路径 已定义 — 是否是绝对的(以驱动器号或 \ 开头) Windows,或 Unix/Linux 系统上的 /)或相对于当前 目录(以 . 或 .. 开头) — include_path 将被忽略 共。例如,如果文件名以 ../ 开头,则解析器 将在父目录中查找以查找请求的文件。 “

什么的父目录?

disc@puff:~/php$ ls 
a.php  data  include 

disc@puff:~/php$ tree 
. 
├── a.php 
├── data 
│   └── d.php 
└── include 
    ├── b.php 
    └── c.php 
2 directories, 4 files 

disc@puff:~/php$ cat a.php 
a.php is including include/b.php ... 
<?php include "include/b.php" ?> 

disc@puff:~/php$ cat include/b.php 
b.php is including c.php and ../data/d.php ... 
<?php include "c.php" ?> 
<?php include "../data/d.php" ?> 

disc@puff:~/php$ cat include/c.php 
c.php 

disc@puff:~/php$ cat data/d.php 
d.php 

disc@puff:~/php$ php a.php 
a.php is including include/b.php ... 
b.php is including c.php and ../data/d.php ... 
c.php 
PHP Warning:  include(../data/d.php): failed to open stream: No 
such file or directory in /home/disc/php/include/b.php on line 3 
PHP Warning:  include(): Failed opening '../data/d.php' for 
inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /home/ 
disc/php/include/b.php on line 3 

disc@puff:~/php$ 

Why does include "c.php" succeed but include "../data/d.php" fail?

http://www.php.net/manual/en/function.include.php mentions: "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 to find the requested file. "

Parent directory of what?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

遥远的她2024-11-21 19:31:10

路径始终与被调用的脚本相关。在你的示例中,c.php 被加载,因为“.” (当前目录)始终位于 include_path 中。

要解决此问题,您可以使用 dirname(__FILE__) 来始终了解文件本身的目录。 (您在其中写入FILE的文件)

或者您可以使用dirname($_SERVER['SCRIPT_FILENAME'])来始终获取校准脚本的目录。

paths are always relative to the script which got called. in your example c.php is loaded because "." (current directory) is always in the include_path.

to fix this you can use dirname(__FILE__) to always know the directory of the file itself. (the file in which you write FILE)

or you can use dirname($_SERVER['SCRIPT_FILENAME']) to alwys get the directory of the caling script.

小清晰的声音2024-11-21 19:31:10

当您从 a.php 开始时,您应该在 a.php 中定义包含目录:

define('MY_INCLUDES', dirname(__FILE__) . '/include/');
define('MY_DATA', dirname(__FILE__) . '/data/');

然后包含具有绝对路径的文件:

include(MY_INCLUDES . 'b.php');
include(MY_DATA . 'c.php');

As you're starting with a.php, you should define the include directories in a.php:

define('MY_INCLUDES', dirname(__FILE__) . '/include/');
define('MY_DATA', dirname(__FILE__) . '/data/');

Afterwards include the files with absolute paths:

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