我在 Debian Squeeze 上运行的文档/home/www/example.com/www
http://www.example.com/
。
/home/www/example.com/
www/
index.php
php/
include_me.php
在 php 中.ini我取消了注释并更改为:
include_path =".:/home/www/example.com"
在脚本索引中.php在 www 中,我有require_once("/php/include_me.php")
.我从 PHP 得到的输出是:
Warning: require_once(/php/include_me.php) [function.require-once]: failed to open stream: No such file or directory in /home/www/example.com/www/index.php on line 2
Fatal error: require_once() [function.require]: Failed opening required '/php/include_me.php' (include_path='.:/home/www/example.com') in /home/www/example.com/www/index.php on line 2
如您所见,包含路径是根据错误正确设置的。但是如果我这样做require_once("../php/include_me.php");
,它就会起作用。因此,包含路径一定有问题。
有谁知道我能做些什么来修复它?
php/
是相对路径,使用当前目录作为起点
./php/
是相对路径,并显式声明当前目录 (.) 作为起点
/php/
是一个不是相对路径,这意味着它的起点是顶级目录(根目录/)
来自include
的文档:
如果定义了路径 - 无论是绝对路径(在Windows上以驱动器号或''开头,在Unix/Linux系统上以/开头)还是相对于当前目录(以.或..开头),则include_path将被完全忽略。例如,如果文件名以 .. 开头。/,解析器将在父目录中查找请求的文件。
由于在本例中指定了绝对路径,因此将忽略include_path
。
我同意 Mihai Stancu 的观点,但我想补充一点,包含使用 dirname(__FILE__) 可能是更好的做法,这样当目录四处移动时,代码就不会中断。 这将充当绝对路径,但允许您将它们视为本地路径。
"/php/include_me.php")的解释:
您将包含路径设置为"/home/www/example.com/",但这是/home/www 的子目录。您的require_once正在寻找/php/include_me.php。
/
home/
www/
example.com/
php/
www/
php/
include_me.php
在你的require调用中有一个前面的/,它正在寻找/php/include_me.php。要查找/home/www/example.com/php/include_me.php,您需要:
require_once('php/include_me.php');
您还可以将包含路径设置为:
include_path =".:/home/www/example.com/:/home/www/example.com/www/:/home/www/example.com/php/"
您的要求上的开头斜杠一次将从根目录搜索...尝试:
require_once('php/include_me.php');
这是试图从字面上获取文件系统上的/php 文件,而不是您的 Web 目录。
require_once("/php/include_me.php");