【问题标题】:require_once function error when creating webpage in new folder在新文件夹中创建网页时出现 require_once 函数错误
【发布时间】:2017-01-04 21:51:20
【问题描述】:

我正在运行 wamp64 以在本地托管我的网站(这是一个用于测试是否有帮助的 vbulletin 论坛)。

我正在尝试创建一个位于子文件夹而不是网站根目录中的新网页,但是我遇到了错误。 我要创建的网页是 addons 文件夹内的 index.php 网页。

我的文件结构是:

C:
-wamp64
--www(网站根目录)
--global.php
---插件
----index.php
----图片
---包括
----config.php

我用于插件的代码 - index.php 是:

    <?php 
          error_reporting(E_ALL & ~E_NOTICE); 

       define('NO_REGISTER_GLOBALS', 1); 
       define('THIS_SCRIPT', 'addons'); 

       $phrasegroups = array(); 

       $specialtemplates = array(); 

       $globaltemplates = array( 
       'ADDONSTESTPAGE' 
       ); 

       $actiontemplates = array(); 

       require_once('/../global.php');

       eval('$navbar = "' . fetch_template('navbar') . '";'); 
       eval('print_output("' . fetch_template('ADDONSTESTPAGE') . '");'); 

   ?> 

当我在 www 文件夹中创建一个新页面时,它运行良好(除了我使用 require_once('/global.php'); 而不是 require_once('/../global.php');。 但是,因为这是在一个子文件夹中,所以我遇到了以下错误,因为 global.php 需要的文件一旦在它们内部有自己的 require_once 函数,并且这些文件在它们内部有自己的 require_once 函数,等等。

这是我现在转到 localhost/addons/ 时遇到的错误:

警告:require_once(C:\wamp64\www\addons/includes/init.php): 失败 打开流:C:\wamp64\www\global.php 中没有这样的文件或目录 第 20 行

致命错误:require_once():需要打开失败 'C:\wamp64\www\addons/includes/init.php' (include_path='.;C:\php\pear') 在 C:\wamp64\www\global.php 第 20 行

这里是 global.php 的第 13-20 行:

// identify where we are
define('VB_AREA', 'Forum');

define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));

//     #############################################################################
// Start initialisation
require_once(CWD . '/includes/init.php');

如何在不弄乱使用 global.php 文件的其他文件的情况下解决这个问题?我听说 realpath 是一个常见的解决方案,但我是 php 新手,不完全确定如何在这种情况下使用它。

感谢所有帮助,非常感谢,如果您需要更多信息,请随时询问!

好的,我可以通过将 require_once(CWD . '/includes/init.php'); 更改为 require_once(__DIR__ . '/includes/init.php'); 来解决该错误。

现在我收到此错误:

警告:require_once(C:\wamp64\www\addons/includes/class_core.php):无法打开流:第 51 行的 C:\wamp64\www\includes\init.php 中没有这样的文件或目录

致命错误:require_once():在 C:\wamp64\www 中打开所需的 'C:\wamp64\www\addons/includes/class_core.php' (include_path='.;C:\php\pear') 失败\includes\init.php 第 51 行

Class_core.php 存在于包含文件夹中。

init.php的第51行是:

require_once(CWD . '/includes/class_core.php');

我尝试将其更改为require_once(__DIR__ . '/includes/class_core.php');,但它破坏了整个网站。

【问题讨论】:

标签: php require-once


【解决方案1】:

global.php 假设服务器进程从主 C:/wamp64/www 目录运行,但由于您从插件文件夹运行,CWD 将设置为 C:/wamp64/www/addon ,因此所有错误.

要解决这个问题,请在 index.php 中执行

1) 恢复您在 global.php 和 init.php 等中对 VBulletin 所做的所有更改...

2) 将当前工作目录设置为根目录,可以在 index.php 顶部的任何 require_once 之前使用 chdir('../')

3) 在你的 index.php 中需要 global.php require_once('global.php');

第 2 步有效,因为现在 php 将根目录视为工作目录。

【讨论】:

  • 我有什么办法可以使页面标题中的图像在 URL 上没有文件夹前缀?例如,标题中的图像链接到localhost/addons/images/bluefox/misc/logo.png 而不是localhost/images/bluefox/misc/logo.png
  • 在 html w3schools.com/tags/att_base_href.asp 的头部使用 base href 属性,即 使所有 url 都相对于首页。
  • 不,chdir 和本地路径不可靠。这里解释了正确的方法:stackoverflow.com/questions/36577020/…
  • @VicSeedoubleyew 这个问题有上下文。因此,您不只是对其应用通用解决方案。 VBulletin 论坛不是他的代码,如果他在 VBulletin 论坛代码中进行更改以实现上述目的,那么当他安装更新时会发生什么,所有这些都会被覆盖。
  • @VicSeedoubleyew 这就是为什么我给了他一个独立于 vbulletin 代码的答案。该答案中提出的任何解决方案都必须由 vbulletin 实施,例如通过define('CWD', __DIR__);,即使 vbulletin 更新他们的代码以使用正确的方法,我的答案仍然有效。
猜你喜欢
  • 2014-05-16
  • 1970-01-01
  • 2021-06-27
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 2012-08-27
  • 1970-01-01
相关资源
最近更新 更多