require_once 无需包含路径即可工作
我有文件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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
设置空包含路径后尝试 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).可能性不大,但也许其他包含的文件之一更改了当前工作目录:
Long shot but maybe one of the other included files changed the current working directory:
为了便于讨论,如果将 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?也许文件 1 的路径中有一个与文件 2 同名的文件。
Perhaps you have a file with the same name as file 2 in the path of file 1.