On various pages throughout my PHP web site and in various nested directories I want to include a specific file at a path relative to the root.
What single command c
I combined the above suggestions and came up with this.
In my config.php
file, I define ROOT as:
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
Then I use it in my succeding files:
include_once(ROOT."/someFile.php");
include_once(ROOT."/includes/someFile.php");
If you give include() or require() (or the *_once versions) an absolute pathname, that file will be included. An absolute pathname starts with a "/" on unix, and with a drive letter and colon on Windows.
If you give a relative path (any other path), PHP will search the directories in the configuration value "include_path" in order, until a match is found or there are no more directories to search.
So, in short, to include an absolute filename, give an absolute filename. See also the function realpath().
If you want to set your own include "root", have a look at this question (specifically my answer of course :-)
This works.
<?php include('c:/inetpub/example.com/includes/analytics.php'); ?>
I'll have to make the c:/inetpub/example.com/
part some kind of global variable so it's somewhat portable from server to server.