Home > Net >  How to include those files which internally include other files?
How to include those files which internally include other files?

Time:11-16

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.

CodePudding user response:

in file config config1.php remove directory config


<?php

require_once './config2.php';

CodePudding user response:

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.

  •  Tags:  
  • php
  • Related