简体   繁体   English

父文件夹包含的文件,另一个文件夹包含的文件

[英]Included file from a parent folder that includes a file from another folder

The title is hard to understand, but my question is easy: 标题很难理解,但我的问题很简单:

I have: 我有:

config.php config.php

include('config_mysql.php');
include('language/english.php');

then I have: index.php 然后我有: index.php

include('config.php');

and then: /ajax/ajax_vote.php 然后: /ajax/ajax_vote.php

include('../config.php');

Why index.php includes both config_mysql.php and language/english.php but ajax/ajax_vote.php only includes the config_mysql.php ? 为什么index.php包含config_mysql.phplanguage/english.phpajax/ajax_vote.php仅包含config_mysql.php

The base folder for include() is allways the base folder of the original script, so the include() s inside an include() d script will be non-intuitive: 对于基本文件夹include()是永诺原始脚本的基础文件夹,因此include()秒的内部include() d脚本将是非直观的:

ajax/ajax_vote.php will (try to) include ajax/ajax_vote.php将(尝试)包括

ajax/../config.php
  -> ajax/sonfig_mysql.php
     ajax/language/english.php

You might want to look at 您可能想看看

$BASEDIR=dirname(__FILE__);
include("$BASEDIR/config_mysql.php");
include("$BASEDIR/language/english.php");

and friends. 和朋友。

The reason is, the language/english.php is not in the same directory as config.php . 原因是language/english.phpconfig.php不在同一目录中。 Also, the /ajax/ajax_vote.php is somewhere outside from the root folder. 另外,/ /ajax/ajax_vote.php在根文件夹之外。

But still, PHP manages to include files correctly. 但是,PHP仍设法正确地包含文件。 I don't understand why the /ajax/ajax_vote.php is not including the file language/english.php . 我不明白为什么/ajax/ajax_vote.php不包含文件language/english.php Correct me if I misunderstood the question. 如果我误解了这个问题,请纠正我。

What Eugen said is true, this is because of the base path not being the same as your included script. Eugen所说的是正确的,这是因为基本路径与所包含的脚本不同。

Also it seems from my tests (because I encountered a similar problem) that php include() will store another temporary include_path when including files, this is why ajax_vote.php could include config_mysql.php. 同样从我的测试(因为我遇到了类似的问题)看来,php include()在包含文件时将存储另一个临时的include_path,这就是为什么ajax_vote.php可以包含config_mysql.php的原因。 To convince yourself, try to move config_mysql.php to your ajax folder: it will work the same, and config.php will be able to include it (but only when executing ajax_vote.php, NOT index.php!). 为了说服自己,请尝试将config_mysql.php移到您的ajax文件夹中:它会以相同的方式工作,并且config.php将能够包含它(但仅当执行ajax_vote.php时,而不是index.php!)。 In this case, you have two include paths: '/ajax/' and '/'. 在这种情况下,您有两个包含路径:“ / ajax /”和“ /”。

However, when you use a path (not merely a filename to include, but also specify a folder in your string), the include path is only based on the executing script (eg: only '/ajax/'). 但是,当您使用路径(不仅包括要包含的文件名,还指定字符串中的文件夹)时,包含路径仅基于执行脚本(例如:仅'/ ajax /')。

You can fix this behaviour by doing: 您可以通过以下方法解决此问题:

include(dirname(__FILE__).'/language/english.php'); // don't forget the prepended '/'

Or just: 要不就:

include('/language/english.php'); // here again the prepended '/'

However I warn you that I don't fully understand yet why the second method works, you may be safer by using the first one with dirname(). 但是,我警告您,我尚不完全了解为什么第二种方法行得通,将第一种方法与dirname()一起使用可能会更安全。

/EDIT: I just found out that include('/path/to/file') is probably a bug on Windows OSes: on Windows it's equal to dirname(__ FILE__ ), but on UNIX it's equal to root path. / EDIT:我刚刚发现include('/ path / to / file')在Windows操作系统上可能是一个错误:在Windows上它等于dirname(__ FILE__),但是在UNIX上它等于根路径。 So dirname(__ FILE__ ) is definately more reliable. 因此dirname(__ FILE__)绝对更可靠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM