relative path / absolute path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jerry M. Gartner

    relative path / absolute path

    Greetings:

    What is the best way to resolve paths within a document regardless of what
    path it is opened under? For example: I have x.php and it contains <img
    src="images..." >, (amongst other things, like php code), which resolves the
    correct image path when opened under / but when x.php is read into a file
    under /dir the image no longer resolves, for obvious reasons. With absolute
    paths, this isn't an issue but it is with relative paths. Although I can
    think of a kludge, or two, I figure that there is a more elegant solution.
    Thanks in advance.

    --

    Regards,

    Jerry M. Gartner


  • David Haynes

    #2
    Re: relative path / absolute path

    Jerry M. Gartner wrote:[color=blue]
    > Greetings:
    >
    > What is the best way to resolve paths within a document regardless of what
    > path it is opened under? For example: I have x.php and it contains <img
    > src="images..." >, (amongst other things, like php code), which resolves the
    > correct image path when opened under / but when x.php is read into a file
    > under /dir the image no longer resolves, for obvious reasons. With absolute
    > paths, this isn't an issue but it is with relative paths. Although I can
    > think of a kludge, or two, I figure that there is a more elegant solution.
    > Thanks in advance.
    >[/color]
    If I understand you correctly, this is really a web server issue and is
    handled by the 'Alias' function in Apache (other servers may call it
    something else). This function allows you to set a path prefix - say,
    /images - and have all references to an image resolve to the same place.

    So in your code you would have <img src="/images/foo.gif"> and it would
    resolve to /my_web/images/foo.gif regardless of where it was referenced
    in your web document tree.

    Aliases may also be used for css, javascript or any other commonly
    shared code elements.

    Hope this helps.
    -david-

    Comment

    • R. Rajesh Jeba Anbiah

      #3
      Re: relative path / absolute path

      Jerry M. Gartner wrote:[color=blue]
      > Greetings:
      >
      > What is the best way to resolve paths within a document regardless of what
      > path it is opened under? For example: I have x.php and it contains <img
      > src="images..." >, (amongst other things, like php code), which resolves the
      > correct image path when opened under / but when x.php is read into a file
      > under /dir the image no longer resolves, for obvious reasons.[/color]
      <snip>

      I don't understand your problem. But, I prefer relative path and
      had no issues with it.

      --
      <?php echo 'Just another PHP saint'; ?>
      Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

      Comment

      • Jan Thomä

        #4
        Re: relative path / absolute path

        On Sun, 18 Jun 2006 10:24:20 GMT Jerry M. Gartner wrote:[color=blue]
        > [...] For example: I have x.php and it contains <img
        > src="images..." >, (amongst other things, like php code), which resolves the
        > correct image path when opened under / but when x.php is read into a file
        > under /dir the image no longer resolves, for obvious reasons. With absolute
        > paths, this isn't an issue but it is with relative paths.[/color]

        Well to be on the safe side use absolute paths, this will definitely work.
        To keep your scripts maintainable, make a configuration file where you will
        put the paths in. E.g

        config.php

        $IMG_BASE_URL=" http://www.my-domain.com/images/";


        and in your php files you write...

        <img src="<?php echo $IMG_BASE_URL; ?>myImage001.jp g"/>

        That way you can even change the image path later on (e.g. put all images
        on a different server or load them with a script for load balancing or
        whatever) and they will always work. I use this solution for my sites (well
        in fact i use some method) like

        function imglnk( $filename ) {
        global $IMG_BASE_URL;
        return $IMG_BASE_URL.$ filename;
        }

        and then
        <img src="<?php imglnk( "myImage001.jpg " );?>/>

        which isn't that kludgy and allows for arbitrary changes of the image
        structure later on.

        Best regards,
        Jan Thomä

        Comment

        • Colin McKinnon

          #5
          Re: relative path / absolute path

          Jan Thomä wrote:
          [color=blue]
          > On Sun, 18 Jun 2006 10:24:20 GMT Jerry M. Gartner wrote:[color=green]
          >> [...] For example: I have x.php and it contains <img
          >> src="images..." >, (amongst other things, like php code), which resolves
          >> the
          >> correct image path when opened under / but when x.php is read into a
          >> file
          >> under /dir the image no longer resolves, for obvious reasons. With
          >> absolute paths, this isn't an issue but it is with relative paths.[/color]
          >
          > Well to be on the safe side use absolute paths, this will definitely work.
          > To keep your scripts maintainable, make a configuration file where you
          > will put the paths in. E.g
          >[/color]
          <snip>
          I would second this approach - in larger applications this has often been
          one of the first 'include' files I've implemented, usually declaring the
          path both in terms of its URL and the filesystem namespace:

          // note - be consistent about trailing dir seperator
          $image_url_pref ix="http://my.server.com/~colin/project/images/";
          $image_file_pre fix="/home/colin/public_html/project/images/";
          ....

          Of course, you'll then realise that the included file itself has a relative
          and absolute path....but the advantage of this approach is that you can use
          the path relative to one of the directories declared in the include_path
          config file setting which removes the problem.

          C.


          Comment

          • Jerry Stuckle

            #6
            Re: relative path / absolute path

            Jerry M. Gartner wrote:[color=blue]
            > Greetings:
            >
            > What is the best way to resolve paths within a document regardless of what
            > path it is opened under? For example: I have x.php and it contains <img
            > src="images..." >, (amongst other things, like php code), which resolves the
            > correct image path when opened under / but when x.php is read into a file
            > under /dir the image no longer resolves, for obvious reasons. With absolute
            > paths, this isn't an issue but it is with relative paths. Although I can
            > think of a kludge, or two, I figure that there is a more elegant solution.
            > Thanks in advance.
            >[/color]

            First of all, this isn't a PHP question - <img statements are html, whether
            they're included in PHP or not. Now - if you were talking about include(),
            require_once(), etc., you 'd be talking php.

            The easiest way I've found is to just use absolute URL's for the images - i.e.

            <img src="/images...">

            Works everyplace on the site and you don't need to include extra files.


            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Geoff Muldoon

              #7
              Re: relative path / absolute path

              jstucklex@attgl obal.net says...[color=blue]
              > Jerry M. Gartner wrote:[color=green]
              > >
              > > What is the best way to resolve paths within a document regardless of what
              > > path it is opened under? For example: I have x.php and it contains <img
              > > src="images..." >, (amongst other things, like php code), which resolves the
              > > correct image path when opened under / but when x.php is read into a file
              > > under /dir the image no longer resolves, for obvious reasons. With absolute
              > > paths, this isn't an issue but it is with relative paths. Although I can
              > > think of a kludge, or two, I figure that there is a more elegant solution.
              > > Thanks in advance.
              > >[/color]
              >
              > First of all, this isn't a PHP question - <img statements are html, whether
              > they're included in PHP or not. Now - if you were talking about include(),
              > require_once(), etc., you 'd be talking php.
              >
              > The easiest way I've found is to just use absolute URL's for the images - i.e.
              >
              > <img src="/images...">
              >
              > Works everyplace on the site and you don't need to include extra files.[/color]

              And in some circumstances (particularly with images) will result in more
              efficient client-side caching.

              GM

              Comment

              • Geoff Berrow

                #8
                Re: relative path / absolute path

                Message-ID: <rrWdnYXXsKRASQ jZnZ2dnUVZ_t2dn Z2d@comcast.com > from Jerry
                Stuckle contained the following:
                [color=blue]
                >The easiest way I've found is to just use absolute URL's for the images - i.e.
                >
                > <img src="/images...">
                >
                >Works everyplace on the site and you don't need to include extra files.[/color]

                I've always found a problem with that when working offline. How do you
                get around that?

                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                • Jerry Stuckle

                  #9
                  Re: relative path / absolute path

                  Geoff Berrow wrote:[color=blue]
                  > Message-ID: <rrWdnYXXsKRASQ jZnZ2dnUVZ_t2dn Z2d@comcast.com > from Jerry
                  > Stuckle contained the following:
                  >
                  >[color=green]
                  >>The easiest way I've found is to just use absolute URL's for the images - i.e.
                  >>
                  >> <img src="/images...">
                  >>
                  >>Works everyplace on the site and you don't need to include extra files.[/color]
                  >
                  >
                  > I've always found a problem with that when working offline. How do you
                  > get around that?
                  >[/color]

                  Don't know what problem you would have, unless you're trying to load the file
                  directly instead of through a webserver.

                  I always use a webserver for testing, though. Even have one going on my main
                  development system.

                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstucklex@attgl obal.net
                  =============== ===

                  Comment

                  • Bent Stigsen

                    #10
                    Re: relative path / absolute path

                    Geoff Berrow wrote:
                    [color=blue]
                    > Message-ID: <rrWdnYXXsKRASQ jZnZ2dnUVZ_t2dn Z2d@comcast.com > from Jerry
                    > Stuckle contained the following:
                    >[color=green]
                    >>The easiest way I've found is to just use absolute URL's for the images -
                    >>i.e.
                    >>
                    >> <img src="/images...">
                    >>
                    >>Works everyplace on the site and you don't need to include extra files.[/color]
                    >
                    > I've always found a problem with that when working offline. How do you
                    > get around that?[/color]

                    Aah, easily "fixed": DocumentRoot "/"

                    :)

                    --
                    /Bent

                    Comment

                    • Chung Leong

                      #11
                      Re: relative path / absolute path

                      Jan Thomä wrote:[color=blue]
                      > Well to be on the safe side use absolute paths, this will definitely work.
                      > To keep your scripts maintainable, make a configuration file where you will
                      > put the paths in. E.g
                      >
                      > config.php
                      >
                      > $IMG_BASE_URL=" http://www.my-domain.com/images/";[/color]

                      If you have ever had to use a reverse-proxy on a web application, you'd
                      realize what an evil absolute paths are.

                      Comment

                      • R. Rajesh Jeba Anbiah

                        #12
                        Re: relative path / absolute path

                        Geoff Berrow wrote:[color=blue]
                        > Message-ID: <rrWdnYXXsKRASQ jZnZ2dnUVZ_t2dn Z2d@comcast.com > from Jerry
                        > Stuckle contained the following:
                        >[color=green]
                        > >The easiest way I've found is to just use absolute URL's for the images - i.e.
                        > >
                        > > <img src="/images...">
                        > >
                        > >Works everyplace on the site and you don't need to include extra files.[/color]
                        >
                        > I've always found a problem with that when working offline. How do you
                        > get around that?[/color]

                        Glad to see professor after long time; hope economy is fine
                        there. I don't understand what sort of problem are you facing. But, in
                        HTML instead of using <img src="/images/..." />, if you use <img
                        src="images/..." /> you'll be better off.

                        --
                        <?php echo 'Just another PHP saint'; ?>
                        Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

                        Comment

                        • Andy Jeffries

                          #13
                          Re: relative path / absolute path

                          On Sun, 18 Jun 2006 20:18:19 -0400, Jerry Stuckle wrote:[color=blue][color=green][color=darkred]
                          >>>The easiest way I've found is to just use absolute URL's for the images
                          >>>- i.e.
                          >>>
                          >>> <img src="/images...">
                          >>>
                          >>>Works everyplace on the site and you don't need to include extra files.[/color]
                          >>
                          >> I've always found a problem with that when working offline. How do you
                          >> get around that?
                          >>[/color]
                          > Don't know what problem you would have, unless you're trying to load the
                          > file directly instead of through a webserver.
                          >
                          > I always use a webserver for testing, though. Even have one going on my
                          > main development system.[/color]

                          Exactly, particularly as this is a PHP newsgroup - the next complaint will
                          be "when I work offline, my PHP doesn't execute, it just prints the
                          source code" :-)

                          Cheers,


                          Andy

                          --
                          Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
                          http://www.gphpedit.org | PHP editor for Gnome 2
                          http://www.andyjeffries.co.uk | Personal site and photos

                          Comment

                          • Jerry Stuckle

                            #14
                            Re: relative path / absolute path

                            R. Rajesh Jeba Anbiah wrote:[color=blue]
                            > Geoff Berrow wrote:
                            >[color=green]
                            >>Message-ID: <rrWdnYXXsKRASQ jZnZ2dnUVZ_t2dn Z2d@comcast.com > from Jerry
                            >>Stuckle contained the following:
                            >>
                            >>[color=darkred]
                            >>>The easiest way I've found is to just use absolute URL's for the images - i.e.
                            >>>
                            >>> <img src="/images...">
                            >>>
                            >>>Works everyplace on the site and you don't need to include extra files.[/color]
                            >>
                            >>I've always found a problem with that when working offline. How do you
                            >>get around that?[/color]
                            >
                            >
                            > Glad to see professor after long time; hope economy is fine
                            > there. I don't understand what sort of problem are you facing. But, in
                            > HTML instead of using <img src="/images/..." />, if you use <img
                            > src="images/..." /> you'll be better off.
                            >
                            > --
                            > <?php echo 'Just another PHP saint'; ?>
                            > Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
                            >[/color]

                            Doesn't validate in HTML 4.01 (strict or transitional).

                            --
                            =============== ===
                            Remove the "x" from my email address
                            Jerry Stuckle
                            JDS Computer Training Corp.
                            jstucklex@attgl obal.net
                            =============== ===

                            Comment

                            • Jerry M. Gartner

                              #15
                              Re: relative path / absolute path

                              I understand that this is a php group. Thus the post. The question is
                              relevant to php as I am writing a rather ugly php application - the post
                              from Jan was just what I was looking for. I appreciate everyone's input.

                              --

                              Regards,

                              Jerry M. Gartner

                              "Jerry Stuckle" <jstucklex@attg lobal.net> wrote in message
                              news:rrWdnYXXsK RASQjZnZ2dnUVZ_ t2dnZ2d@comcast .com...[color=blue]
                              > Jerry M. Gartner wrote:[color=green]
                              >> Greetings:
                              >>
                              >> What is the best way to resolve paths within a document regardless of
                              >> what path it is opened under? For example: I have x.php and it contains
                              >> <img src="images..." >, (amongst other things, like php code), which
                              >> resolves the correct image path when opened under / but when x.php is
                              >> read into a file under /dir the image no longer resolves, for obvious
                              >> reasons. With absolute paths, this isn't an issue but it is with
                              >> relative paths. Although I can think of a kludge, or two, I figure that
                              >> there is a more elegant solution. Thanks in advance.
                              >>[/color]
                              >
                              > First of all, this isn't a PHP question - <img statements are html,
                              > whether they're included in PHP or not. Now - if you were talking about
                              > include(), require_once(), etc., you 'd be talking php.
                              >
                              > The easiest way I've found is to just use absolute URL's for the images -
                              > i.e.
                              >
                              > <img src="/images...">
                              >
                              > Works everyplace on the site and you don't need to include extra files.
                              >
                              >
                              > --
                              > =============== ===
                              > Remove the "x" from my email address
                              > Jerry Stuckle
                              > JDS Computer Training Corp.
                              > jstucklex@attgl obal.net
                              > =============== ===[/color]


                              Comment

                              Working...