require_once 无需包含路径即可工作

发布于 2024-09-15 05:13:41 字数 404 浏览 3 评论 0原文

我有文件 1.php,其中包含 2.php 的 require_once (它们都位于不同的文件夹中)。我遇到的问题是 1.php 似乎神奇地包含 2.php,因为 2.php 的路径不在 1.php 中。可以肯定的是,我什至在 require_once '2.php' 之前添加了 set_include_path('.') ,但它仍然有效......是否有一些明显的我遗漏的东西,或者这很奇怪吗?

编辑:

//-- file 1.php
//-- long list of requires...
set_include_path('.');
echo get_include_path();
require_once '2.php';

当 1.php 和 2.php 位于不同的文件夹中时,上述工作正常。

I have file 1.php having a require_once for 2.php (both of them are in different folders). The issue I am having is that 1.php seems to magically include 2.php since the path for 2.php is not in 1.php. Just to be sure, I even added a set_include_path('.') before the require_once '2.php', but it still works... Is there something obvious I am missing or is this plain weird??

Edit:

//-- file 1.php
//-- long list of requires...
set_include_path('.');
echo get_include_path();
require_once '2.php';

The above works fine while 1.php and 2.php are in different folders.

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

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

发布评论

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

评论(5

多彩岁月 2024-09-22 05:13:41

PHP 中有一些包含魔法,我以前见过。
我相信有些与自动加载相关的东西。
它总是在包含正在运行的类的文件所在的文件夹中查找

There is some include magic in PHP, i've met it before.
Something autoload related, I believe.
It always looks in the folder where file with running class resides

玻璃人 2024-09-22 05:13:41

设置空包含路径后尝试 echo get_include_path() ,它可能会在其他地方设置(例如在 Web 服务器配置文件中)。

Try echo get_include_path() after you set your empty include path, it may be set somewhere else (like in the web server config file).

赢得她心 2024-09-22 05:13:41

可能性不大,但也许其他包含的文件之一更改了当前工作目录:

<?php
echo 'cwd at the beginning of 1.php: ', getcwd(), "\n";
//-- file 1.php
//-- long list of requires...
set_include_path('.');
echo 'include_path: ', get_include_path(), " \n";
echo 'cwd: ', getcwd(), "\n";
require_once '2.php';

Long shot but maybe one of the other included files changed the current working directory:

<?php
echo 'cwd at the beginning of 1.php: ', getcwd(), "\n";
//-- file 1.php
//-- long list of requires...
set_include_path('.');
echo 'include_path: ', get_include_path(), " \n";
echo 'cwd: ', getcwd(), "\n";
require_once '2.php';
幻梦 2024-09-22 05:13:41

为了便于讨论,如果将 2.php 放在与 1.php 相同的位置,是否会包含新文件而不是旧文件?

你能告诉 use 你的 open_basedir 的值是多少吗?

For the sake of argument, if you put a 2.php in the same location as 1.php does the new file get included instead of the old one?

Can you tell use what the value of your open_basedir is?

尾戒 2024-09-22 05:13:41

也许文件 1 的路径中有一个与文件 2 同名的文件。

Perhaps you have a file with the same name as file 2 in the path of file 1.

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