1

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.

1 Answers1

0

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.

dfasoro
  • 241
  • 2
  • 5
  • is there any way for me to make it so that the images in the header of the page don't have the folder prefix on the URL? for example the images in the header are linking to http://localhost/addons/images/bluefox/misc/logo.png instead of http://localhost/images/bluefox/misc/logo.png – user6768030 Aug 29 '16 at 02:52
  • Use the base href attribute in the head of the html http://www.w3schools.com/tags/att_base_href.asp i.e. to make all urls relative to the top-page. – dfasoro Aug 29 '16 at 03:06
  • No, chdir and local paths are not reliable. The correct ways to do are explained here : http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Aug 29 '16 at 17:55
  • @VicSeedoubleyew there is a context to the question. So, you don't just apply generic solutions to it. The VBulletin forum is not his code, if he makes changes in the VBulletin forum code to achieve the above, what happens when he installs an update and all that gets overwritten. – dfasoro Aug 29 '16 at 18:42
  • @VicSeedoubleyew that was why I gave him an answer that works independently of the vbulletin code. Any of the solutions proposed in that answer will have to be implemented by vbulletin e.g. by doing `define('CWD', __DIR__);` even when vbulletin updates their code to use the right method, my answer will still work regardless. – dfasoro Aug 29 '16 at 18:46
  • @dfasoro sorry, i worded that wrong i guess, they are linking to the correct place, but the images themselves are broken because it is trying to use this image localhost/addons/images/bluefox/misc/logo.png instead of this image (the correct one) localhost/images/bluefox/misc/logo.png how do i fix that :)? It looks like replacing all the a href with base href in my header fixed most of the images but broke all of the links – user6768030 Aug 29 '16 at 19:05
  • yes, the ` – dfasoro Aug 29 '16 at 19:08
  • oh okay so i just have to makesure im only using it for the images hrefs and it will work? – user6768030 Aug 29 '16 at 19:09
  • for example this: $vboptions[bbtitle] I changed a href to base href, and it fixes the image but breaks the link, should I just add a duplicate around the ? – user6768030 Aug 29 '16 at 19:11
  • no, no, You are not to change ` – dfasoro Aug 29 '16 at 19:17
  • ooooooh I get it now yea for some reason i was thinking header instead of ,I get it now :P, thank you for all of the help and sorry to comment instead of chat, it wont let me chat because my rep is only 8 :( – user6768030 Aug 29 '16 at 19:19
  • @dfasoro, indeed `define('CWD', __DIR__);` seems a lot more robust to me than using chdir(). why not adivse him that ? – Vic Seedoubleyew Sep 04 '16 at 17:42
  • @VicSeedoubleyew I didn't advise him to do that because then he will be editing someone else's code. He will then need to keep track of all such edits for whenever he installs a vbulletin update. It is just like editing wordpress core code just to achieve some extra stuffs instead of writing a proper plugin. When wordpress auto-update runs, all your edits will be lost and your application will be broken – dfasoro Sep 05 '16 at 20:38
  • Can't he define the constant in his own code ? Wouldn't that override vbulletin's definition ? – Vic Seedoubleyew Sep 07 '16 at 19:46
  • @VicSeedoubleyewas you can see from vbulletin code `define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));` there is no if (defined('CWD')) to do that check. – dfasoro Sep 07 '16 at 21:33
  • @VicSeedoubleyew what do you think PHP will do if you try to define an already defined constant. Error hint: `Notice: Constant CWD already defined in ***.php on line xxx` – dfasoro Sep 07 '16 at 21:37
  • Indeed. I am surprised that such a framework relies on such a bad practice. Isn't there an option to turn it off, and use paths relative to installation directory instead ? – Vic Seedoubleyew Sep 10 '16 at 09:31