require_once with subfolders

Mariam picture Mariam · Feb 24, 2013 · Viewed 7.3k times · Source

I have Folder Home with two subfolders like the following

+Home  
 +include  
   - membersite_config.php

+iDiscover  
   -index.php

in index.php I added the require_once script to access membersite_config.php

<?PHP

require_once($_SERVER['DOCUMENT_ROOT'].'/include/membersite_config.php');
?>

I get the following error : Warning: require_once(./include/fg_membersite.php): failed to open stream: No such file or directory in D:\Home\include\membersite_config.php on line 2
Fatal error: require_once(): Failed opening required './include/fg_membersite.php' (include_path='.;C:\php\pear') in D:\Home\include\membersite_config.php on line 2

The error says No such file or directory while the The path "D:\Home\include\membersite_config.php" is correct . When I move index.php to be under the root , the page works well.

I also tried to use the following code as described here but it gives the same error

<?PHP


define('ROOT', 'D:Home\\');
require_once(ROOT ."/include/membersite_config.php");
?>

Edit :

I too tried require_once("../include/membersite_config.php"); And it gives the same error

Thanks in advance

Answer

Winston picture Winston · Feb 24, 2013

You must include like this

require_once(dirname(__FILE__) . '/include/membersite_config.php');

Because DocumentRoot can be not set in httpd.conf