PHP:包含相对于调用文件而不是根文件的文件?

发布于 2024-08-22 08:56:56 字数 342 浏览 1 评论 0原文

好吧,假设我在同一目录中有一个名为 fileA.php 的文件,我还有另一个名为 fileB.php 的文件,

所以在 fileA 中有这个 require_once('fileB. php') 请注意,我没有指定文件路径,因为这两个文件位于同一目录中。

现在,如果 fileA 包含到位于其他位置的另一个文件中,则它无法包含 fileB,因为父文件(包含 fileA 的文件)与 fileB 不在同一目录中。

那么如何才能使 fileA 加载 fileB 无论 fileA 包含在何处?

这有道理吗?

谢谢!!

OK say I have a file called fileA.php in the same directory I have another file called fileB.php

so inside fileA there is this require_once('fileB.php') notice I did not specify a file path because both files are in the same directory.

Now if fileA is included into another file that is located somewhere else, it fails to include fileB because the parent file (the file that included fileA) is not in the same directory as fileB.

So how can I make it so that fileA loads fileB no matter where fileA is included into?

Does that make sense?

Thanks!!

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

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

发布评论

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

评论(2

陌路黄昏 2024-08-29 08:56:56

假设 fileA.php 和 fileB.php 都保证位于同一目录中,这应该可以工作:

// fileA.php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR .'fileB.php')

Assuming both fileA.php and fileB.php are guaranteed to be in the same directory, this should work:

// fileA.php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR .'fileB.php')
友谊不毕业 2024-08-29 08:56:56

http://php.net/manual/en/function.include.php# 78716

您可以将包含根设置为您的网站根。

http://php.net/manual/en/function.include.php#78716

You can set the include root to your website root.

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