Including files with images and relative/absolute addresses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Taurins

    #1

    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."imagenam e.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.



  • Fred H

    #2
    Re: Including files with images and relative/absolute addresses

    [color=blue]
    > Any suggestions as to how to get images displaying when including a
    > header.php file in a lower lever directory?[/color]

    How about just

    $path = "/images/";
    $image = $path."imagenam e.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.sa y("frode at age dee dee dot en oh");
    }

    Comment

    • Peter Taurins

      #3
      Re: Including files with images and relative/absolute addresses

      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" <secret@nospam. com> wrote in message
      news:opr3vhptdv qlpr0e@news.mim er.no...[color=blue]
      >[color=green]
      > > Any suggestions as to how to get images displaying when including a
      > > header.php file in a lower lever directory?[/color]
      >
      > How about just
      >
      > $path = "/images/";
      > $image = $path."imagenam e.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.sa y("frode at age dee dee dot en oh");
      > }[/color]


      Comment

      • dr zoidberg

        #4
        Re: Including files with images and relative/absolute addresses

        Peter Taurins wrote:
        [color=blue]
        > 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."imagenam e.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?[/color]

        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."imagenam e.jpg";
        echo "<img src=\"$image\"> ";

        Comment

        Working...