I want to include a file using something like: <?php include 'header.txt'; ?> The problem is that my header.txt is in the root directory, and I am placing this code in various directories and sub directories. How do I state the path such that the same header.txt is always included?
You will need to get the full php path to the specific file from the server root. If you have shell access you should be able to just go into the directory with the header.txt file and type "pwd" to get the current path, but if not then make a php file in the same directory as the header.txt file. Put this in the contents: <?php echo __DIR__; ?> PHP: Navigate to that page in the browser to get the path to the current directory with symlinks resolved. Copy that string (we'll call it $dir). Now to include that text file anywhere, use this code: include $dir.'/header.txt'; PHP:
__DIR__ constant was added in PHP 5.3.0, maybe you're using older version of PHP. In older versions, the same behaviour will be achieved by dirname(__FILE__). But I don't think this solves your problem. __DIR__points to the directory where your PHP script is, not to the root. As I said, use absolute (full) path to the header.txt file and it will work...
Try echoing $_SERVER['DOCUMENT_ROOT'], this will output the path to the document root of your web hosting account. You can then use this variable along with the 'header.txt' text to get a full, absolute path to use for the include or require statement.
But if you use $_SERVER['DOCUMENT_ROOT'], then the header.txt file msut be visible "from the outside"...