I have the following directory structure:
home/admin/super/
In home I have a file called config.php
<?php
//Get relative path - for PHP files
$documentRoot = $_SERVER['DOCUMENT_ROOT'].'/mwo2015/';
include($documentRoot.'library/constants.php');
echo $paypalID;
?>
and echo $paypalID
is echoed;
In path home/admin/ I have a file called access-control.php
<?php
//Common files
include('../config.php');
?>
and echo $paypalID
is echoed again.
In path home/admin/super/ I have a file called output.php
<?php
include('../access-control.php');
?>
Nothing is output and I get the following error in my error_log of path
PHP Warning: include() [<a href='function.include'>function.include</a>]: Failed opening '../config.php' for inclusion (include_path='.:/usr/lib64/php:/usr/share/pear')
Try this..
The "relative include path" is not shifted to the included file
The solution is work with absolute paths and you can combine with relatives and absolute paths using dirname(__FILE__)
, and you get the directory that contains the file.
In within include file you can use following
require_once dirname(__FILE__) . '/yourfile.php';
require_once dirname(__DIR__) . '/yourfile.php';
or
include dirname(__FILE__) . "/yourfile.php";
include dirname(__DIR__) . "/yourfile.php";
The problem your having is this:
Relative paths use the entry point. So if you start and go to home/admin/super/output.php you start from there.
so home/admin/super.
include('../access-control.php');
goes to home/admin
The include in this file goes to home/admin but it should go to home so to fix this you could do:
../../config.php
This is the reason why you build your application around one entry point or make a global root and webroot to use throughout your application.