require_once被忽略
Windows 上的 php 出现奇怪的问题...我的应用程序加载一个“核心”文件,该文件加载设置文件、注册自动加载、进行初始化等。在核心文件的顶部我有 include_once("config.php"); 这对于当前目录中的任何内容都适用,如果我包含来自单独目录的核心文件,尽管它只是默默地忽略配置文件的包含...以前有人见过这个吗?
/webroot/core.php
<?php
require_once "config.php";
//register autoloads
//do some initialization... standard stuff
?>
/webroot/config.php
<?php
define("WEB_ROOT","/webroot");
define("DB_USER","root");
// ... more stuff...
?>
/webroot/admin/index.php
<?php
require_once("../dbconnect.php");
echo WEB_ROOT;
// print string literal WEB_ROOT rather than value in config.php
?>
我的理解是文件操作是相对于发出请求的文件的目录而言的,不应该 require_once("config.php")选择相对于 core.php 的文件? 这段代码在 Mac 或 Linux 上的工作方式正如我所期望的那样,但在 Windows 上根本不起作用(如果我将 require 更改为使用完整路径或 ../,它就工作了)
真正疯狂的是 require_once("config .php") 不会抛出任何错误,但里面的代码都不会被执行!
strange problem with php on windows... my application loads a 'core' file that loads a settings file, registers autoloads, does initialization etc. at the top of the core file I have include_once("config.php"); this works fine for anything in the current directory, if I include the core file from a separate directory though it just silently ignores the include of the config file... has anyone seen this before?
/webroot/core.php
<?php
require_once "config.php";
//register autoloads
//do some initialization... standard stuff
?>
/webroot/config.php
<?php
define("WEB_ROOT","/webroot");
define("DB_USER","root");
// ... more stuff...
?>
/webroot/admin/index.php
<?php
require_once("../dbconnect.php");
echo WEB_ROOT;
// print string literal WEB_ROOT rather than value in config.php
?>
My understanding is that file operations are relative to the directory of the file making the request, shouldn't the require_once("config.php") select the file relative to core.php? This code works just as I'd expect on a mac or Linux but not at all on windows, (if I change the require to use the full path or ../, it works)
The real madness is that the require_once("config.php") doesn't throw any errors but none of the code inside is executed!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 PHP 默默地忽略错误,请尝试将其放在所请求的文件的顶部:
现在所有错误都应该可见。
然后,最佳实践(根据我的说法)是在请求的文件中(而不是在任何包含的文件中)定义一些具有应用程序根的完整文件系统路径的常量。 因此,在
index.php
中,如果应用程序根目录为/webroot/admin
:之后,当您包含某些内容时,请使用此常量。 如果目录结构是:
并且您希望将
config.php
包含到index.php
中:在
core.php
中包含config .php
它将是:等等。
使用绝对文件系统路径可以防止任何歧义。
If PHP silently ignore errors, try to put at the top of file that is requested:
Now all errors should be visible.
Then the best practice (according to me) is to in file that is requested (not in any of included files) define some constant with full filesystem path for application root. So in
index.php
if application root is/webroot/admin
:After that when you include something, use this constant. If the directory structure is:
And you want to include
config.php
intoindex.php
:In
core.php
to includeconfig.php
it would be:And so on.
Using absolute filesystem paths will prevent any ambiguities.
如果要使用相对于执行包含的文件的路径来包含文件,则必须在前面添加完整路径,如下所示:
If you want to include a file using a path relative to the file doing the inclusion, you'll have to prepend the full path, like this:
也许您遇到了 PHP 的 open_basedir 限制? 为了尝试阻止某些恶意脚本,PHP 有一个“安全模式”,其中包括限制文件的加载位置。 如果 xampp 默认打开此功能,您可能会遇到它。
您可能还想检查网络服务器的错误日志,看看是否有任何失败的迹象(并打开错误报告,最好是 E_ALL,直到问题得到解决)。
Perhaps you're running into PHP's open_basedir restriction? To attempt to prevent some malicious scripts, PHP has a 'safe mode' that, among other things, limits where files can be loaded from. If xampp had this turned on by default you might have run into it.
You might also want to check your webserver's error logs to see if there are any indications of failure there (and turn on error reporting, preferably to E_ALL, until you get the issue sorted).