Using a root-relative path when including files


I have the following files:


I want to include config.php in top.php.

I know I can use the following line to include it:

include( “…/config.php” )

But, I was wondering why I can’t use:

include( “/config.php” )

Is there an alternative function that can be used that recognises a root-relative path?

Using that style of relative path uses the root of the physical drive, not your server document root.

Try using include($_SERVER[‘DOCUMENT_ROOT’].‘/config.php’)

For simplicity lets say your web documents reside here: C:\server\htdocs\myapp
when you use just “/” or “\” at the beginning of the path it is equivalent of typing “C:\”
this applies to Linux too I’m just using Window paths for simplicity.

Thanks for that guys.

Using $_SERVER[‘DOCUMENT_ROOT’] is a satisfactory workaround :slight_smile: