Search code examples
phprequire-once

require_once function error when creating webpage in new folder


I am running wamp64 to locally host my website (it is a vbulletin forums used for testing if that helps at all).

I am trying to create a new webpage that is located in a subfolder instead of the root of the website, however I am running into errors. The webpage I am trying to create is the index.php webpage inside of the addons folder.

My file structure is:

C:
-wamp64
--www (website root)
--global.php
---addons
----index.php
----images
---includes
----config.php

The code I am using for addons - index.php is:

    <?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') . '");'); 

   ?> 

When I create a new page in the www folder, it works perfectly (except I use require_once('/global.php'); instead of require_once('/../global.php');. However, because this is in a subfolder, I am running into the following error due to the files that the global.php is requiring once having their own require_once functions inside of them and those files have their own require_once functions inside of them, etc.

Here is the error I am getting now when I go to localhost/addons/:

Warning: require_once(C:\wamp64\www\addons/includes/init.php): failed to open stream: No such file or directory in C:\wamp64\www\global.php on line 20

Fatal error: require_once(): Failed opening required 'C:\wamp64\www\addons/includes/init.php' (include_path='.;C:\php\pear') in C:\wamp64\www\global.php on line 20

Here are lines 13-20 of global.php:

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

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

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

How do I solve this without messing up other files that use the global.php file? I have heard realpath is a common solution for this, but I am new to php and not exactly sure how to use it in this scenario.

All help is appreciated, thank you very much, if you need any more information feel free to ask!

Okay I was able to solve that error by changing require_once(CWD . '/includes/init.php'); to require_once(__DIR__ . '/includes/init.php');.

Now I am getting this error:

Warning: require_once(C:\wamp64\www\addons/includes/class_core.php): failed to open stream: No such file or directory in C:\wamp64\www\includes\init.php on line 51

Fatal error: require_once(): Failed opening required 'C:\wamp64\www\addons/includes/class_core.php' (include_path='.;C:\php\pear') in C:\wamp64\www\includes\init.php on line 51

Class_core.php exists in the includes folder.

Line 51 of init.php is:

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

I tried changing it to require_once(__DIR__ . '/includes/class_core.php'); but it breaks the entire website.


Solution

  • global.php assumes the server process is running from the main C:/wamp64/www directory but because you are running from the addon folder, CWD will be set to C:/wamp64/www/addon , hence all the errors.

    To resolve this, do, in your index.php

    1) Revert all the changes you have made to VBulletin in your global.php and init.php etc...

    2) set your current working directory to the root directory, you can use chdir('../') at the top of index.php before any require_once

    3) require global.php in your index.php require_once('global.php');

    Step 2 works because now php now sees the root directory as the working directory.