Log in

View Full Version : include relative



bluewalrus
01-14-2011, 06:34 AM
Is there a way to make extensions within an include relative to the path they were included in?

For example


<?php include("../header.php");?>

which contains
<link href="css/home.css" rel="stylesheet" type="text/css">

when it should also be


<link href="../css/home.css" rel="stylesheet" type="text/css">

james438
01-14-2011, 07:59 AM
Try using something along the lines of this:

$root="$_SERVER[DOCUMENT_ROOT]";
include $root.'/css/home.css';

bluewalrus
01-14-2011, 01:46 PM
I'm not including the css file, I'm including a file in another directory with links to css and js in that dir as well.

djr33
01-14-2011, 08:24 PM
Here are some tricks:
include(dirname(__FILE__).'/file.php');
That will include a file relative to the current file, regardless of whether the file is the original location for the request or not.

For HTML, just use a HREF/SRC starting in '/':
<... src="/dir1/dir2/....">
That's not as efficient as a relative path, but it will always work regardless of how it was included into a page.