>>> How to get root directory in PHP for making include code portable
Posted on 2008-11-04
I've just started using includes in PHP and I need some help! : )
Here's what I'm trying to do. I have a bit of code which was redundant on many scripts of the site. These scripts are in many different directories.
I have created an include file which contains the code. Now I want to create a line of code which I can place into all the scripts which need the include code.
Ideally it would look something like this:
include ( [site root string or equivalent] . "/includes/code.php");
I know I can enter the path in manually but I want this to be portable.
And I know I can use relative paths but that would mean I'd have to count how many folders each script is nested in and probably make mistakes which will cause problems.
What I need is to have one line of code which I can use on all scripts no matter where they are on the site relative to the root directory and which can be portable.
I've been looking for hours and it seems many people have the same problem but nobody has an easy solution. You'd think PHP would have a $_SERVER['rootdir'] variable built in but it doesn't look like that's the case.