PHP 是否包含相对于文件或调用代码的路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7378814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are PHP include paths relative to the file or the calling code?
提问by Yarin
I'm having trouble understanding the ruleset regarding PHP relative include paths. If I run file A.PHP- and file A.PHP includes file B.PHP which includes file C.PHP, should the relative path to C.PHP be in relation to the location of B.PHP, or to the location of A.PHP? That is, does it matter which filethe include is called from, or only what the current working directory is- and what determines the current working directory?
我无法理解有关 PHP 相对包含路径的规则集。如果我运行文件 A.PHP- 并且文件 A.PHP 包含包含文件 C.PHP 的文件 B.PHP,那么 C.PHP 的相对路径应该与 B.PHP 的位置有关,还是与 A 的位置有关.PHP?也就是说,从哪个文件调用包含重要吗,或者只有当前工作目录是什么 - 以及什么决定了当前工作目录?
回答by Pekka
It's relative to the main script, in this case A.php. Remember that include()
just inserts code into the currently running script.
它相对于主脚本,在本例中为 A.php。请记住,这include()
只是将代码插入当前运行的脚本中。
That is, does it matter which file the include is called from
也就是说,从哪个文件调用 include 有关系吗?
No.
不。
If you want to makeit matter, and do an include relative to B.php, use the __FILE__
constant (or __DIR__
since PHP 5.2 IIRC) which will always point to the literal current file that the line of code is located in.
如果你想要做它的事,做一个包括相对于B.php,使用__FILE__
恒定的(或__DIR__
因为PHP 5.2 IIRC),这将始终指向字面当前文件的代码行位于英寸
include(dirname(__FILE__)."/C.PHP");
回答by Yarin
@Pekka got me there, but just want to share what I learned:
@Pekka 让我到了那里,但只想分享我学到的东西:
getcwd()
returns the directory where the file you started executing resides.
getcwd()
返回您开始执行的文件所在的目录。
dirname(__FILE__)
returns the directory of the file containing the currently executing code.
dirname(__FILE__)
返回包含当前正在执行的代码的文件的目录。
Using these two functions, you can always build an include path relative to what you need.
使用这两个函数,您始终可以构建与所需内容相关的包含路径。
e.g., if b.php and c.php share a directory, b.php can include c.php like:
例如,如果 b.php 和 c.php 共享一个目录,则 b.php 可以包含 c.php,例如:
include(dirname(__FILE__).'/c.php');
no matter where b.php was called from.
无论 b.php 是从哪里调用的。
In fact, this is the preferred way of establishing relative paths, as the extra code frees PHP from having to iterate through the include_path in the attempt to locate the target file.
事实上,这是建立相对路径的首选方式,因为额外的代码使 PHP 不必在尝试定位目标文件时遍历 include_path。
Sources:
资料来源:
Difference Between getcwd() and dirname(__FILE__) ? Which should I use?
getcwd() 和 dirname(__FILE__) 之间的区别?我应该使用哪个?
回答by Johnny Wong
If include path doesn't start with
./
or../
, e.g.:include 'C.php'; // precedence: include_path (which include '.' at first), // then path of current `.php` file (i.e. `B.php`), then `.`.
If include path starts with
./
or../
, e.g.:include './C.php'; // relative to '.' include '../C.php'; // also relative to '.'
如果包含路径不以
./
or开头../
,例如:include 'C.php'; // precedence: include_path (which include '.' at first), // then path of current `.php` file (i.e. `B.php`), then `.`.
如果包含路径以
./
或开头../
,例如:include './C.php'; // relative to '.' include '../C.php'; // also relative to '.'
The .
or ..
above is relative to getcwd()
, which defaults to the path of the entry .php
file (i.e. A.php
).
所述.
或..
以上是相对于getcwd()
,缺省条目的路径.php
文件(即A.php
)。
Tested on PHP 5.4.3 (Build Date : May 8 2012 00:47:34).
在 PHP 5.4.3 上测试(构建日期:2012 年 5 月 8 日 00:47:34)。
(Also note that chdir()
can change the output of getcwd()
.)
(另请注意,chdir()
可以更改 . 的输出getcwd()
。)
回答by Johnny Wong
The accepted answer of Pekka is incomplete and, in a general context, misleading. If the file is provided as a relative path, the called language construct include
will search for it in the following way.
Pekka 的公认答案是不完整的,并且在一般情况下具有误导性。如果文件作为相对路径提供,则被调用的语言结构include
将按以下方式搜索它。
First, it will go through the paths of the environment variable include_path
, which can be set with ini_set
. If this fails, it will search in the calling script's own directory dirname(__FILE__)
(__DIR__
with php >= 5.3.) If this also fails, only then it will search in the working directory ! It just turns out that, by default, the environment variable include_path
begins with .
, which is the current working directory. That is the only reason why it searches first in the current working directory. See http://php.net/manual/en/function.include.php.
首先,它会遍历环境变量的路径include_path
,可以用ini_set
. 如果失败,它将在调用脚本自己的目录中搜索dirname(__FILE__)
(__DIR__
使用 php >= 5.3。)如果这也失败,那么它才会在工作目录中搜索!事实证明,默认情况下,环境变量include_path
以 开头.
,这是当前工作目录。这是它首先在当前工作目录中搜索的唯一原因。请参阅http://php.net/manual/en/function.include.php。
Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.
根据给定的文件路径包含文件,如果没有给出,则指定 include_path。如果在 include_path 中找不到该文件,include 将在失败之前最终检查调用脚本自己的目录和当前工作目录。
So, the correct answer to the first part of the question is that it does matter where is located the included calling script. The answer to the last part of the question is that the initialworking directory, in a web server context, is the directory of the called script, the script that includes all the others while being handled by PHP. In a command line context, the initial working directory is whatever it is when php is invoked at the prompt, not necessarily the directory where the called script is located. The currentworking directory, however, can be changed at run time with the PHP function chdir
. See http://php.net/manual/en/function.chdir.php.
因此,问题第一部分的正确答案是包含的调用脚本的位置确实很重要。问题最后一部分的答案是,在 Web 服务器上下文中,初始工作目录是被调用脚本的目录,该脚本包含所有其他脚本,同时由 PHP 处理。在命令行上下文中,初始工作目录是在提示符处调用 php 时的任何目录,不一定是被调用脚本所在的目录。在当前的工作目录,但是,可以在与PHP函数运行时间而改变chdir
。请参阅http://php.net/manual/en/function.chdir.php。
This paragraph is added to comment on other answers. Some have mentioned that relying on include_path
is less robust and thus it is preferable to use full paths such as ./path
or __DIR__ . /path
. Some went as far as saying that relying on the working directory .
itself is not safe, because it can be changed. However, some times, you need to rely on environment values. For example, you might want set include_path
empty, so that the directory of the calling script is the first place that it will search, even before the current working directory. The code might be already written and updated regularly from external sources and you do not want to reinsert the prefix __DIR__
each time the code is updated.
添加此段落是为了评论其他答案。有些人提到依赖include_path
不太可靠,因此最好使用完整路径,例如./path
或__DIR__ . /path
。有些人甚至说依赖工作目录.
本身并不安全,因为它是可以更改的。但是,有时,您需要依赖环境值。例如,您可能希望设置为include_path
空,以便调用脚本的目录是它将搜索的第一个位置,甚至在当前工作目录之前。代码可能已经从外部来源定期编写和更新,并且您不希望__DIR__
每次更新代码时都重新插入前缀。
回答by Denis Howe
Short answer: it's relative to the including script.
简短回答:它与包含脚本有关。
TFMexplains it correctly:
TFM正确解释:
If the file isn't found in the include_path, include will check in the calling script's directory and the current working directory
如果在 include_path 中找不到该文件,include 将检查调用脚本的目录和当前工作目录
So, if /app/main.phpsays include("./inc.php")
that will find /app/inc.php.
所以,如果/app/main.php说include("./inc.php")
会找到/app/inc.php。
The ./is not strictly necessary but removes any dependency on include_path.
该./不是绝对必要的,但消除了对包含路径的任何依赖。
I would not rely on finding include files in the current working directory in case someone changes it with chdir()
.
我不会依赖于在当前工作目录中查找包含文件,以防有人使用chdir()
.
回答by Olli
dir
-> a.php
-> c.php
- dir2
-> b.php
To include a
in b
you need to include("../a.php");
要包含a
在b
你需要include("../a.php");
To include b
in c
you need to include("dir2/b.php");
要包含b
在c
你需要include("dir2/b.php");