skip to Main Content

This is my file structure

+-- config
|   +-- config1.php
|   +-- config2.php
+-- test
|   +-- test1
|      +-- innerRunner.php
+-- outerRunner.php

this is config1.php

<?php
require_once 'config/config2.php';

this is config2.php

<?php
function runConfig2(){
    echo "control in config2";
}
runConfig2();

this is innerRunner.php

<?php
require_once("../../config/config1.php");

this is outerRunner.php

<?php
require_once("config/config1.php");

When i exceute outerRunner.php then everything works fine but when i run innerRunner.php
it shows error

Warning: require_once(config/config2.php): Failed to open stream: No such file or directory in /var/www/config/config1.php on line 2

Fatal error: Uncaught Error: Failed opening required 'config/config2.php' (include_path='.:/usr/local/lib/php') in /var/www/config/config1.php:2 Stack trace: #0 /var/www/test/test1/innerRunner.php(2): require_once() #1 {main} thrown in /var/www/config/config1.php on line 2

Can anyone help me with this?
Here is the link of sandbox for the same:
Link

PS : I can’t make any changes in config1 or config2, i need to do something with innerRunner.php only.

2

Answers


  1. in file config config1.php remove directory config

    
    <?php
    
    require_once './config2.php';
    
    
    Login or Signup to reply.
  2. You can do this in innerRunner.php

    <?php
    #change the current working dir 
    chdir('/var/www');
    require_once("config/config1.php");
    #change it back to the current dir
    chdir(__DIR__);
    

    But this is really a bad approach to do something like this.

    Normaly you should only do require_once 'config2.php'; (config1.php) in the first place.

    There is no real reason to do require_once 'config/config2.php'; there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search