PHP是否包括相对于文件或调用代码的路径?

发布于 2025-02-08 13:46:56 字数 187 浏览 2 评论 0原文

我很难理解有关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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

呆橘 2025-02-15 13:46:56

它相对于主脚本,在这种情况下为A.Php。请记住,include()只需将代码插入当前运行的脚本即可。

也就是说,重要的是从

调用哪个文件

如果您想 make ,那么inclusta in noce not the conse 至关重要吗常数(或__ dir __以来是PHP 5.2 IIRC),它将始终指向代码行所在的字面当前文件。

include(dirname(__FILE__)."/C.PHP");

It's relative to the main script, in this case A.php. Remember that include() just inserts code into the currently running script.

That is, does it matter which file the include is called from

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.

include(dirname(__FILE__)."/C.PHP");
挽清梦 2025-02-15 13:46:56

@pekka把我带到了那里,但只想分享我学到的东西:

()返回您开始执行的文件所在的目录。

dirname(__ file __ file __) 返回的目录包含当前执行代码的文件。

使用这两个功能,您始终可以构建相对于您需要的路径。

例如,如果b.php和c.php共享目录,则B.PHP可以包括c.php,例如:

include(dirname(__FILE__).'/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:

include(dirname(__FILE__).'/c.php');

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__)

好听的两个字的网名 2025-02-15 13:46:56
  1. 如果inclage路径不是./或 ../ ,例如:

     包括'c.php'; //优先:include_path(首先包括'。'),
                      //然后当前`.php`文件(即b.php`)的路径,然后``。
     
  2. 如果inclage路径以./或<代码> ../ ,例如:

     包括'./c.php'; //相对于'。'
    
     包括'../c.php'; //也相对于'。'。
     

..是相对的到getCWD(),默认到条目.php file(IE a.php)的路径,例如某些Web服务器apache。

在PHP 5.4.3(构建日期:2012年5月8日00:47:34)上进行了测试。

(还请注意,chdir()可以更改getcwd()的输出。

  1. 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 `.`.
    
  2. If include path starts with ./ or ../, e.g.:

     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) 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 of getcwd().)

素食主义者 2025-02-15 13:46:56

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

文件是根据给出的文件路径包含的,或者如果没有给出,
指定的include_path。如果在
include_path,include Will Will Will Will将在调用脚本的
失败之前的目录和当前工作目录。

因此,问题的第一部分的正确答案是,它确实在何处所包含的调用脚本都重要。问题的最后一部分的答案是,在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 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.

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.

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 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.

2025-02-15 13:46:56

简短答案:这是相对于脚本的。

tfm 正确解释了它:

如果在include_path中找不到该文件,则会在调用脚本的目录和当前工作目录

中检查

因此,如果/app/main.php inclage(“ ./” .php“)可以找到/app/inc.php

./ 并不是严格必要的,但可以消除对Incluest_path的任何依赖。

如果有人用chdir()将其更改,我不会依靠查找将文件包含在当前工作目录中。

Short answer: it's relative to the including script.

TFM explains it correctly:

If the file isn't found in the include_path, include will check in the calling script's directory and the current working directory

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().

难忘№最初的完美 2025-02-15 13:46:56
dir
-> a.php
-> c.php

- dir2 
-> b.php

要包含a in b您需要include(“ ../ a.php”);

include bc中,您需要include(“ dir2/b.php”);

dir
-> a.php
-> c.php

- dir2 
-> b.php

To include a in b you need to include("../a.php");

To include b in c you need to include("dir2/b.php");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文