470,833 Members | 1,391 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Including files with images and relative/absolute addresses

Hi there.

I have an included file (header.php) that contanis a reference to a graphic.
If I stay at the root level, then I can control the relative path of the
image.
eg. images/imagename.jpg

However, if I include this file (header.php) in a lower level directory then
of course, I end up with no image displaying as the relative path is no
longer correct.

Currently I am using the following to provide an absolute path to the image.

$path = "http://".$_SERVER["HTTP_HOST"]."/images/";
$image = $path."imagename.jpg";
echo "<img src=\"$image\">";

This works fine, but looks klunky.

I would like to avoid specifying an absolute hardcoded path name so I can
load this function on any site I choose.

Any suggestions as to how to get images displaying when including a
header.php file in a lower lever directory?

Your assistance is appreciated.
Peter.

Jul 17 '05 #1
3 2966
Any suggestions as to how to get images displaying when including a
header.php file in a lower lever directory?


How about just

$path = "/images/";
$image = $path."imagename.jpg";

Wouldn't that achieve what you want? But if you need a
complete URL, then I think you're doing it just right
the way you are now.

But unless you are running a whole range of hosts that
all have the "/images/"-dir, I can't see what's wrong
with specifying a hardcoded, server specific path, since
you'll have to specify the "images"-part of it anyway?

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}
Jul 17 '05 #2
Well, I learn something (fundamental) every day.
I didn't realise what the / did at the front.
And now I do.
Thanks muchly.
PWT
"Fred H" <se****@nospam.com> wrote in message
news:op**************@news.mimer.no...
Any suggestions as to how to get images displaying when including a
header.php file in a lower lever directory?


How about just

$path = "/images/";
$image = $path."imagename.jpg";

Wouldn't that achieve what you want? But if you need a
complete URL, then I think you're doing it just right
the way you are now.

But unless you are running a whole range of hosts that
all have the "/images/"-dir, I can't see what's wrong
with specifying a hardcoded, server specific path, since
you'll have to specify the "images"-part of it anyway?

--
Fred H

void FredH::Contact() {
TextToSpeach.say("frode at age dee dee dot en oh");
}

Jul 17 '05 #3
Peter Taurins wrote:
Hi there.

I have an included file (header.php) that contanis a reference to a graphic.
If I stay at the root level, then I can control the relative path of the
image.
eg. images/imagename.jpg

However, if I include this file (header.php) in a lower level directory then
of course, I end up with no image displaying as the relative path is no
longer correct.

Currently I am using the following to provide an absolute path to the image.

$path = "http://".$_SERVER["HTTP_HOST"]."/images/";
$image = $path."imagename.jpg";
echo "<img src=\"$image\">";

This works fine, but looks klunky.

I would like to avoid specifying an absolute hardcoded path name so I can
load this function on any site I choose.

Any suggestions as to how to get images displaying when including a
header.php file in a lower lever directory?


I think that it is the best to always use absolute path. That way you
can move your files within folders, without breaking urls.

If your path is always the same you can always use:

$path = "/images/";
$image = $path."imagename.jpg";
echo "<img src=\"$image\">";
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Geoff Berrow | last post: by
7 posts views Thread by mark | last post: by
4 posts views Thread by Bernie Raffe | last post: by
2 posts views Thread by vinit_mahajan99 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.