【发布时间】:2011-04-01 18:03:40
【问题描述】:
我有几个独立的网站,它们位于不同的目录中。对于它们共同的包含,我将它放在其余部分所在的根目录中。
user@hostname:/var/www$ ls
website_1 website_2 website_3 common_files
我想包含一个 Zend 包,所以我有我的包含路径
ini_set("include_path", get_include_path() . ":/var/www/common_files/Zend");
require_once("Mail.php");
Mail.php 加载正常,但在某处有这一行
require_once 'Zend/Mail/Transport/Abstract.php';
这个错误
Warning: require_once(Zend/Mail/Transport/Abstract.php): failed to open stream: No such file or directory in var/www/common_files/Zend/Mail.php on line 26
所以 php 不会递归地进入包含路径的目录结构。我是否必须将 Zend 移动到每个网站目录中,说明每个包含的路径,还是什么?
BTW 摘要确实存在:
user@host:/var/www/common_files/Zend$ tree -d
...
`-- Mail/Transport
|-- Mail/Transport/Abstract.php
|-- Mail/Transport/Exception.php
|-- Mail/Transport/Sendmail.php
`-- Mail/Transport/Smtp.php
9 directories, 32 files
【问题讨论】:
-
尝试使用常量
PATH_SEPARATOR而不是冒号...所以get_include_path() . PATH_SEPARATOR . '/var/www/common_viles/Zend'); -
这已经困扰我太多次了。包含文件时,它从与包含文件相同的路径运行。除非它们在同一个文件夹中,否则包含文件中的任何相对路径都会导致错误。有谁知道这个问题的解决方案吗?
-
@Adrian,我认为为此使用 dirname 是常见的做法。
require_once dirname(__FILE__) . '/relative/path/../here'; -
我想解决方案是库文件中不应包含硬编码:P
-
@Brandon 啊,是的,编译时路径看起来是绝对的,对吧?谢谢!
标签: php server-side-includes include-path