php require 中的文件路径

发布于 2024-10-22 06:55:44 字数 231 浏览 2 评论 0原文

我继承了一些代码:

include('../cfg/db_setup.php');
require('./fpdf/fpdf.php');

我知道 ../cfg 意味着从当前目录开始,然后向上一层并向下进入 cfg 目录。 ./fpdf 是什么意思?我从未见过在文件路径中使用单点斜杠,并且似乎无法在我们服务器上的任何位置找到 fpdf 目录,但代码正在运行,所以显然它就在那里。

I've inherited some code:

include('../cfg/db_setup.php');
require('./fpdf/fpdf.php');

I know that ../cfg means start in the current directory, then go up one level and down into the cfg dir. What does the ./fpdf mean? I've never seen a single dot slash used in a file path, and can't seem to find the fpdf directory anywhere on our server, but the code is working so apparently it's there somewhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

森罗 2024-10-29 06:55:44

只是一个注释。

相对路径不相对于当前的 include_path。

相对路径,例如 .或 ../ 相对于当前工作目录。如果您在 CGI 或命令行上运行脚本,或者某个脚本被另一个目录中的另一个脚本包含,则工作目录会有所不同。

因此,理论上不可能在不知道上下文的情况下确定这些路径指向哪里:P

可以肯定的是,如果 PHP < 5.3:

include(dirname(__FILE__) . '/../cfg/db_setup.php');
require(dirname(__FILE__) . '/fpdf/fpdf.php');

如果 PHP >= 5.3:

include(__DIR__ . '/../cfg/db_setup.php');
require(__DIR__ . '/fpdf/fpdf.php');

Just a note.

Relative paths don't are relative to the current include_path.

Relative paths, such as . or ../ are relative to the current working directory. The working directory is different if you run a script on CGI or command line or if a script is included by another script in another directory.

Therefore theoretically is not possible to be sure where these paths are pointing to without to know the context :P

To be sure, if PHP < 5.3:

include(dirname(__FILE__) . '/../cfg/db_setup.php');
require(dirname(__FILE__) . '/fpdf/fpdf.php');

If PHP >= 5.3:

include(__DIR__ . '/../cfg/db_setup.php');
require(__DIR__ . '/fpdf/fpdf.php');
后来的我们 2024-10-29 06:55:44

. 被定义为当前文件夹。

因此,如果 PHP 脚本位于 /path/to/script/,那么第二条语句将查找 /path/to/script/fpdf/fpdf.php

. is defined as the current folder.

So, if the PHP script is located at /path/to/script/, then the second statement will look for /path/to/script/fpdf/fpdf.php

执手闯天涯 2024-10-29 06:55:44

./ 是当前目录。 ./fpdf/ 应该与包含文件位于同一路径上,或者位于 php include_path 之外的某个位置。

./ is the current directory. ./fpdf/ should be on the same path as the including file, or somewhere on the off the php include_path.

余生共白头 2024-10-29 06:55:44

./ - 这意味着在当前路径中

./ - this means in the current path

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