tags:

views:

22

answers:

3

I'm using simple include files for common elements on a mostly static website. I do so like this

<?php include('/inc/header.php'); ?>

But this doesn't work unless i remove the leading slash, and then it only works on pages in the root directory. What am I doing wrong?

+3  A: 

/... is an absolute path on unix systems. To specify a relative path use ./.... That will be relative to the called file's directory.

nikic
+1  A: 

'/' means the real server root directory, not the web document root. If you need the relocatability use:

 include("{$_SERVER['DOCUMENT_ROOT']}/inc/header.php");
mario
I went a step further and did this: set_include_path("{$_SERVER['DOCUMENT_ROOT']}/inc");so that I could call all the includes using just the file name like this:include('filename.php');
Ben
For portability sake, I wouldn't recommend using any of the `$_SERVER` paths... I recommend [`dirname(__FILE__);`](http://stackoverflow.com/questions/2893088/best-method-for-creating-absolute-path-in-php-see-3-methods-listed-inside/2893104#2893104) since it will always work 100% of the time...
ircmaxell
A: 

You have an absolute path - i.e. starts with /. So it is looking in your server root.

Without the slash it is a relative path and will look relative to the PHP file's path.

Jason McCreary