PHP 中的 require_once

发布于 2024-08-29 19:42:01 字数 328 浏览 2 评论 0原文

我有一个 php 文件,其中有一个 require_once 语句(?),然后该文件包含在另外 2 个 php 文件中,一个 php 文件位于子目录中,因此布局如下(“file1”和“file2”包含该文件"included" which require_onces the "required")#

 L--subfolder1
 | L--file1
 L--subfolder2
 | L--required
 L--file2
 L--included

如何从“included”文件中引用“required”文件,以便它可以在 file1 和 file2 中工作?

I have a php file which has a require_once Statement (?) this file is then in included in 2 other php files, one php file is in a sub directory so the layout is like this ("file1" and "file2" include the file "included" which require_onces the "required")#

 L--subfolder1
 | L--file1
 L--subfolder2
 | L--required
 L--file2
 L--included

How can I reference the "required" file from the "included" file so that it will work from both file1 and file2?

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

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

发布评论

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

评论(3

昵称有卵用 2024-09-05 19:42:01

始终使用绝对路径

$_SERVER['DOCUMENT_ROOT']."/included";

$_SERVER['DOCUMENT_ROOT']."/subfolder2/required";

可以在任何地方工作

always use absolute path

$_SERVER['DOCUMENT_ROOT']."/included";

or

$_SERVER['DOCUMENT_ROOT']."/subfolder2/required";

would work from anywhere

无所谓啦 2024-09-05 19:42:01

您必须使用绝对路径才能从任何地方请求具有相同代码的文件。

require_once($_SERVER['DOCUMENT_ROOT'] . '/subfolder2/required');

You have to use absolute path to be able to require the file with the same code from anywhere.

require_once($_SERVER['DOCUMENT_ROOT'] . '/subfolder2/required');
吃→可爱长大的 2024-09-05 19:42:01

您可以将 dirname() 与 __FILE__ 常量结合使用。 (如果我理解正确的话,您会从两个脚本中包含“包含”文件,然后“包含”包含一些文件?)
包含(.php):

require_once( dirname(__FILE__)."/subfolder2/required" );

You can use dirname() in combination with the __FILE__ constant. (If I understand you correctly you're including the file 'included' from both scripts and 'included' then includes some files?)
included(.php):

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