标题很难理解,但我的问题很简单:
我有:
配置.php
include('config_mysql.php');
include('language/english.php');
那么我有:索引.php
include('config.php');
然后:/ajax/ajax_vote.php
include('../config.php');
为什么index.php
既包括config_mysql.php
也包括language/english.php
,ajax/ajax_vote.php
只包括config_mysql.php
?
include()
的基本文件夹是原始脚本的基本文件夹,因此 include()
d 脚本中的include()
将是不直观的:
ajax/ajax_vote.php
将(尝试)包括
ajax/../config.php
-> ajax/sonfig_mysql.php
ajax/language/english.php
你可能想看看
$BASEDIR=dirname(__FILE__);
include("$BASEDIR/config_mysql.php");
include("$BASEDIR/language/english.php");
和朋友。
,language/english.php
与config.php
不在同一个目录中。此外,/ajax/ajax_vote.php
位于根文件夹之外的某个位置。
但是,PHP仍然设法正确包含文件。我不明白为什么/ajax/ajax_vote.php
不包括文件language/english.php
.如果我误解了这个问题,请纠正我。
Eugen 说的是真的,这是因为基本路径与您包含的脚本不同。
此外,从我的测试中(因为我遇到了类似的问题),php include() 在包含文件时会存储另一个临时include_path,这就是为什么ajax_vote.php可以包含config_mysql.php的原因。为了说服自己,请尝试将config_mysql.php移动到您的 ajax 文件夹:它将工作相同,并且 config.php 将能够包含它(但仅在执行 ajax_vote.php 时,而不是 index.php!在本例中,您有两个包含路径:"/ajax/"和"/"。
但是,当您使用路径(不仅是要包含的文件名,还要在字符串中指定文件夹)时,包含路径仅基于执行脚本(例如:仅"/ajax/")。
您可以通过执行以下操作来修复此行为:
include(dirname(__FILE__).'/language/english.php'); // don't forget the prepended '/'
或者只是:
include('/language/english.php'); // here again the prepended '/'
但是我警告您,我还不完全了解为什么第二种方法有效,使用第一种方法和dirname()可能会更安全。
/编辑:我刚刚发现include('/path/to/file')可能是Windows操作系统上的一个错误:在Windows上它等于dirname(__ FILE__),但在UNIX上它等于根路径。所以目录(__FILE__)肯定更可靠。