PHP是否包括相对于文件或调用代码的路径?
我很难理解有关PHP亲戚的规则集包括路径。如果我运行文件a.php-和文件A.PHP包括文件b.php,其中包括文件c.php,则应与b.php的位置相关的相对路径,或与b.php的位置有关.php?也就是说, file include in incluct in from哪个是从当前的工作目录中调用的,以及什么决定当前工作目录的是什么?
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 file the include is called from, or only what the current working directory is- and what determines the current working directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它相对于主脚本,在这种情况下为A.Php。请记住,
include()
只需将代码插入当前运行的脚本即可。它
如果您想 make ,那么inclusta in noce not the conse 至关重要吗常数(或
__ dir __
以来是PHP 5.2 IIRC),它将始终指向代码行所在的字面当前文件。It's relative to the main script, in this case A.php. Remember that
include()
just inserts code into the currently running script.No.
If you want to make it 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.@pekka把我带到了那里,但只想分享我学到的东西:
()
返回您开始执行的文件所在的目录。
dirname(__ file __ file __)
返回的目录包含当前执行代码的文件。使用这两个功能,您始终可以构建相对于您需要的路径。
例如,如果b.php和c.php共享目录,则B.PHP可以包括c.php,例如:
无论何处何时列出b.php。
实际上,这是建立相对路径的首选方法,因为额外的代码从必须通过Include_path迭代以尝试定位目标文件。
来源:
getCwd()和dirname(__ file __ file __)之间的差异?我应该使用哪个?
为什么要使用dirname(__ file __)
@Pekka got me there, but just want to share what I learned:
getcwd()
returns the directory where the file you started executing resides.dirname(__FILE__)
returns the directory of the file containing the currently executing code.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:
no matter where b.php was called from.
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.
Sources:
Difference Between getcwd() and dirname(__FILE__) ? Which should I use?
Why you should use dirname(__FILE__)
如果inclage路径不是
./
或 ../ ,例如:如果inclage路径以
./
或<代码> ../ ,例如:。
或..
是相对的到getCWD()
,默认到条目.php
file(IEa.php
)的路径,例如某些Web服务器apache。在PHP 5.4.3(构建日期:2012年5月8日00:47:34)上进行了测试。
(还请注意,
chdir()
可以更改getcwd()
的输出。If include path doesn't start with
./
or../
, e.g.:If include path starts with
./
or../
, e.g.:The
.
or..
above is relative togetcwd()
, which defaults to the path of the entry.php
file (i.e.A.php
) for some web servers, such as Apache.Tested on PHP 5.4.3 (Build Date : May 8 2012 00:47:34).
(Also note that
chdir()
can change the output ofgetcwd()
.)Pekka的公认答案是不完整的,并且在一般情况下是误导的。如果将文件作为相对路径提供,则称为语言构造将以以下方式搜索它。
首先,它将遍历环境变量
includ_path
的路径,可以使用ini_set
设置。如果失败,它将在调用脚本的目录dirname(__ file __)
(__ dir ______
带有php&gt; = 5.3。)中搜索。在工作目录中搜索!事实证明,默认情况下,环境变量indubl_path
以。
开始,这是当前的工作目录。这是它首先在当前工作目录中搜索的唯一原因。参见 http://php.net/manual/manual/enual/en/function.include.include.php 。因此,问题的第一部分的正确答案是,它确实在何处所包含的调用脚本都重要。问题的最后一部分的答案是,在Web服务器上下文中,初始工作目录是所谓的脚本的目录,即包含所有其他脚本的脚本,由PHP处理。在命令行上下文中,初始工作目录是在提示下调用PHP的任何内容,而不一定是所谓的脚本所在的目录。但是,当前工作目录可以在运行时使用PHP函数
chdir
更改。请参阅。添加了本段以对其他答案发表评论。有些人提到,依靠
include_path
不太强大,因此最好使用./ path
或____ __ dir__等完整路径。 /路径
。有些人甚至说依靠工作目录。
本身并不安全,因为它可以更改。但是,有时您需要依靠环境价值。例如,您可能需要设置incept_path
eytic,因此调用脚本的目录是它将搜索的第一个位置,甚至在当前工作目录之前。该代码可能已经从外部来源定期编写和更新,并且您不想每次更新代码时重新插入前缀__ dir __
。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.First, it will go through the paths of the environment variable
include_path
, which can be set withini_set
. If this fails, it will search in the calling script's own directorydirname(__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 variableinclude_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.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 initial working 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 current working directory, however, can be changed at run time with the PHP function
chdir
. See 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 setinclude_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.简短答案:这是相对于脚本的。
tfm 正确解释了它:
因此,如果/app/main.php 说
inclage(“ ./” .php“)
可以找到/app/inc.php 。./ 并不是严格必要的,但可以消除对Incluest_path的任何依赖。
如果有人用
chdir()
将其更改,我不会依靠查找将文件包含在当前工作目录中。Short answer: it's relative to the including script.
TFM explains it correctly:
So, if /app/main.php says
include("./inc.php")
that will find /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()
.要包含
a
inb
您需要include(“ ../ a.php”);
include
b
在c
中,您需要include(“ dir2/b.php”);
To include
a
inb
you need toinclude("../a.php");
To include
b
inc
you need toinclude("dir2/b.php");