require_once被忽略

发布于 2024-08-01 22:43:01 字数 934 浏览 3 评论 0原文

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 技术交流群。

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

发布评论

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

评论(3

∝单色的世界 2024-08-08 22:43:01

如果 PHP 默默地忽略错误,请尝试将其放在所请求的文件的顶部:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

现在所有错误都应该可见。

然后,最佳实践(根据我的说法)是在请求的文件中(而不是在任何包含的文件中)定义一些具有应用程序根的完整文件系统路径的常量。 因此,在 index.php 中,如果应用程序根目录为 /webroot/admin

define('APP_DIR', dirname(__FILE__));

之后,当您包含某些内容时,请使用此常量。 如果目录结构是:

/webroot/
    admin/
        index.php
    config.php
    core.php

并且您希望将 config.php 包含到 index.php 中:

require_once APP_DIR . '/../config.php';

core.php 中包含 config .php 它将是:

require_once APP_DIR . '/../config.php';

等等。

使用绝对文件系统路径可以防止任何歧义。

If PHP silently ignore errors, try to put at the top of file that is requested:

ini_set('display_errors', 'On');
error_reporting(E_ALL);

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:

define('APP_DIR', dirname(__FILE__));

After that when you include something, use this constant. If the directory structure is:

/webroot/
    admin/
        index.php
    config.php
    core.php

And you want to include config.php into index.php:

require_once APP_DIR . '/../config.php';

In core.php to include config.php it would be:

require_once APP_DIR . '/../config.php';

And so on.

Using absolute filesystem paths will prevent any ambiguities.

橘味果▽酱 2024-08-08 22:43:01

如果要使用相对于执行包含的文件的路径来包含文件,则必须在前面添加完整路径,如下所示:

<?php
require_once(dirname(__FILE__) . "/config.php");
?>

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
require_once(dirname(__FILE__) . "/config.php");
?>
烟─花易冷 2024-08-08 22:43:01

也许您遇到了 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).

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