root path doesn't work with php include

Viewed 73289

/ in the beginning of a link to get to the root folder doesn't work in php include.

for example "/example/example.php"

What is the solution?

9 Answers

maybe it's a bit unconventional

If I have a case like

/var/www/namedir/  <= root

/var/www/namedir/example/example.php <= file to include

-- directory when i need the include -- 
/var/www/namedir/dir1/page.php 
/var/www/namedir/dir1/dirA/page.php
/var/www/namedir/dir1/dirB/page.php

the solution that I use is simple. get the path before the "Dir1"

something like this

include (substr(dirname(__FILE__),0,strpos(dirname(__FILE__), '/dir1'))."/example/example.php");

I found it usefull id i need to rename the main subdir for example from

/var/www/namesite/internalDirA/dirToInclude/example.php  
/var/www/namesite/internalDirA/dir1/dirA/example.php 
/var/www/namesite/internalDirA/dir1/dirB/example.php 

TO

/var/www/namesite/dirReserved/dirToInclude/example.php  
/var/www/namesite/dirReserved/dir1/dirA/example.php 
/var/www/namesite/dirReserved/dir1/dirB/example.php 

This answer is not really for the root directory, but one workaround is to use ../ to jump to the parent directory.
Of course, you need to know the file structure for this approach though.

For example, you could use:

include('../parent.php');
include('../../grand_parent.php');
Related