php DOCUMENT_ROOT question

Status
Not open for further replies.

jonathan11

New Member
Hi,

I'm using php include to build the header of all my site pages.
This was fine once all pages were in the main directory and I could display an image in the header like this....
<a href="/"> <img src="img/logo.png" /> </a>

Now I want to introduce pages in another directory. This is where the above code falls down since img/ directory is not at the same level.

So I tried doing this...
$img = $_SERVER['DOCUMENT_ROOT'] . '/img/logo.png';
print "<a href=\"/\"> <img src=\"" . $img . "\" /> </a>";


However I can't get this to display the image, when I look at the source code of the page I see...
<a href="/"> <img src="/home/broadban/public_html/mobileplans.ie/img/logo.png" /> </a>


Any idea why its not displaying the image? this is really bugging me.
Thanks,
Jonathan
 

louie

New Member
Best programming practise is to include the full URL path for the images, CSS

e.g. http:// URL/ IMG FOLDER/img.jpg

you can easily achieve this by defining the path
PHP:
define("HOME","http:// FULL URL.com")
define("IMG_PATH",HOME."/IMG FOLDER");
 
//then you write the img as
$img = IMG_PATH."/logo.png";
echo "<a href='LINK' title='title'><img src='".$img."' alt='title' />";
 

jonathan11

New Member
Thanks Louie for the answer.
I'll stick to full URLs so for the images.
Think I was trying to over complicate things :)
 
Status
Not open for further replies.
Top