Dynamic Image SRC

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ian Rastall

    Dynamic Image SRC

    I would like to use an include for my header and navigation, and
    because subsequent files may be in different directories, I want to
    use a dynamic relative link for the images, sort of like:

    <img src="<? $root ?>/images/foo.gif" />

    I suppose the easiest thing to do is just use absolute links, but
    is there a way to set this up so I can PHP instead?

    TIA
    Ian
    --

  • Erwin Moller

    #2
    Re: Dynamic Image SRC

    Ian Rastall wrote:
    [color=blue]
    > I would like to use an include for my header and navigation, and
    > because subsequent files may be in different directories, I want to
    > use a dynamic relative link for the images, sort of like:
    >
    > <img src="<? $root ?>/images/foo.gif" />
    >
    > I suppose the easiest thing to do is just use absolute links, but
    > is there a way to set this up so I can PHP instead?
    >
    > TIA
    > Ian[/color]


    Hi Ian,

    The question is this: How can PHP know which path (relative or absolute)
    should be added?
    If you can describe it clearly, you can program PHP to do it.
    I am sure of that.

    But you didn't provide us with any usefull information, so I am unsure what
    kind of answer you expect.

    As for the syntax: That is ok, as long as $root contains something that can
    be parsed before the path.

    Regards,
    Erwin Moller

    Comment

    • Ian Rastall

      #3
      Re: Dynamic Image SRC

      In comp.lang.php Erwin Moller wrote:
      [color=blue]
      > But you didn't provide us with any usefull information, so I
      > am unsure what kind of answer you expect.[/color]

      Hi Erwin. I'll try and provide some better information. The
      problem, essentially, is that I have a testing server set up, so
      that the path to the main index file is

      but the URL when it gets on the remote server will be
      gongfamily.net is your first and best source for information about gongfamily. Here you will also find topics relating to issues of general interest. We hope you find what you are looking for!


      Now, my directory tree so far is very simple:

      gongfamily
      -----images
      -----sundry
      -----daevid
      -----gilli
      (and others at the same directory level)

      The main index file has a PHP include in it, which points to
      sundry/header.txt

      In header.txt, I have a lot of image calls, in the form of:

      src="images/foo.gif"

      This works fine with the front page, but in the index page for


      the images are broken, because the links are relative. I could
      make the links absolute, but then I couldn't use my testing
      server. There might be an Apache solution to this, such as
      setting up some sort of alias or virtual root, but that stuff is
      beyond me.

      I hope that helps. I'm tearing my hair out right now. :-)

      Ian
      --

      Comment

      • Erwin Moller

        #4
        Re: Dynamic Image SRC

        Ian Rastall wrote:
        [color=blue]
        > In comp.lang.php Erwin Moller wrote:
        >[color=green]
        >> But you didn't provide us with any usefull information, so I
        >> am unsure what kind of answer you expect.[/color]
        >
        > Hi Erwin. I'll try and provide some better information. The
        > problem, essentially, is that I have a testing server set up, so
        > that the path to the main index file is
        > http://localhost/gongfamily/
        > but the URL when it gets on the remote server will be
        > http://www.gongfamily.net/
        >
        > Now, my directory tree so far is very simple:
        >
        > gongfamily
        > -----images
        > -----sundry
        > -----daevid
        > -----gilli
        > (and others at the same directory level)
        >
        > The main index file has a PHP include in it, which points to
        > sundry/header.txt
        >
        > In header.txt, I have a lot of image calls, in the form of:
        >
        > src="images/foo.gif"
        >
        > This works fine with the front page, but in the index page for
        > http://localhost/gongfamily/daevid/
        >
        > the images are broken, because the links are relative. I could
        > make the links absolute, but then I couldn't use my testing
        > server. There might be an Apache solution to this, such as
        > setting up some sort of alias or virtual root, but that stuff is
        > beyond me.
        >
        > I hope that helps. I'm tearing my hair out right now. :-)
        >
        > Ian[/color]


        Aha, ok:

        You could try several solutions.
        However: in my humble opinion a 'relative path solution' for the images is
        the best solution.
        In that way you don't have to worry about changing location.

        But you end up with 2 (or maybe more) versions of header.txt
        header1.txt
        <img src="/images/foo.gif">

        header2.txt
        <img src="../images/foo.gif">


        If that is no problem, do it that way.

        If you think that is messy, try this:
        (I also use it to include functionsfiles and such)
        I always have an includefile in all my scripts at top.

        In that includefile I have 1 variable that contains the:
        * full URL to root of your application.
        (in the include:)
        $myAppRoot = "http://localhost/gongfamily/";
        Including that variable makes it easy to refer to the imagedirectorie s, no
        matter from where you use them:
        <img src="<?= $myAppRoot ?>/images/layout/version1/foo.gif">

        If you switch your application to another location, you only have only to
        change that variable.

        Of course, you can also use $_SERVER to do the same. It contains all kind of
        usefull information, like the path to the current script. But I prefer 1
        simple variable to set this. Matter of taste.

        Good luck.

        Regards,
        Erwin Moller

        Comment

        • Ian Rastall

          #5
          Re: Dynamic Image SRC

          In comp.lang.php Erwin Moller wrote:
          [color=blue]
          > In that includefile I have 1 variable that contains the:
          > * full URL to root of your application.
          > (in the include:)
          > $myAppRoot = "http://localhost/gongfamily/";
          > Including that variable makes it easy to refer to the
          > imagedirectorie s, no matter from where you use them:
          > <img src="<?= $myAppRoot ?>/images/layout/version1/foo.gif">
          >
          > If you switch your application to another location, you only
          > have only to change that variable.[/color]

          Thank you! That's a good solution. Not entirely hands-off, but
          very easy to modify.
          [color=blue]
          > Of course, you can also use $_SERVER to do the same. It
          > contains all kind of usefull information, like the path to the
          > current script.[/color]

          If possible, could you give me the syntax for that? That sounds
          like the totally hands-off solution. But if not, the first is
          perfect.

          As far as src="/images/foo.gif" I couldn't get that to work, at
          least not locally.

          Thanks again!

          Ian
          --

          Comment

          • Erwin Moller

            #6
            Re: Dynamic Image SRC

            Erwin Moller wrote:

            I wasn't very clear. Some modifications:
            [color=blue]
            >
            > Aha, ok:
            >
            > You could try several solutions.
            > However: in my humble opinion a 'relative path solution' for the images is
            > the best solution.
            > In that way you don't have to worry about changing location.
            >
            > But you end up with 2 (or maybe more) versions of header.txt
            > header1.txt
            > <img src="/images/foo.gif">
            >
            > header2.txt
            > <img src="../images/foo.gif">
            >
            >
            > If that is no problem, do it that way.
            >
            > If you think that is messy, try this:
            > (I also use it to include functionsfiles and such)[/color]

            In which case I have a var in the includefile like:
            $fullPathToRoot = "/home/erwin/public_html/someapp"

            And from a script:
            include "myConffile.php ";
            include "$fullPathToRoo t/include/somefunctions.p hp";

            So the same principle, but not for a http:// but for a normal path.
            [color=blue]
            > I always have an includefile in all my scripts at top.
            >
            > In that includefile I have 1 variable that contains the:
            > * full URL to root of your application.
            > (in the include:)
            > $myAppRoot = "http://localhost/gongfamily/";
            > Including that variable makes it easy to refer to the imagedirectorie s, no
            > matter from where you use them:
            > <img src="<?= $myAppRoot ?>/images/layout/version1/foo.gif">
            >
            > If you switch your application to another location, you only have only to
            > change that variable.
            >
            > Of course, you can also use $_SERVER to do the same. It contains all kind
            > of usefull information, like the path to the current script. But I prefer
            > 1 simple variable to set this. Matter of taste.[/color]

            Try:

            <pre>
            <? var_dump($_SERV ER); ?>
            </pre>

            to see the content.

            check: http://nl2.php.net/reserved.variables
            [color=blue]
            >
            > Good luck.
            >
            > Regards,
            > Erwin Moller[/color]

            Comment

            • Derek Fountain

              #7
              Re: Dynamic Image SRC

              > This works fine with the front page, but in the index page for[color=blue]
              > http://localhost/gongfamily/daevid/
              >
              > the images are broken, because the links are relative. I could
              > make the links absolute, but then I couldn't use my testing
              > server. There might be an Apache solution to this, such as
              > setting up some sort of alias or virtual root, but that stuff is
              > beyond me.[/color]

              My current project lives in a directory called 'dp', and my test machine is
              called 'monkey' (or 'localhost'). I use this function to set a couple of
              path variables:

              function setPaths( &$filePath, &$httpPath )
              {
              if( (strcmp( $_SERVER['SERVER_NAME'], "monkey" ) == 0) ||
              (strcmp( $_SERVER['SERVER_NAME'], "localhost" ) == 0) ) {
              $filePath = "/home/derek/public_html/dp/";
              $httpPath = "/~derek/dp/";
              } else {
              $filePath = "/home/derek/html/www.dp.com/public_html/";
              $httpPath = "/";
              }
              }

              This gets called at the top of each page as part of the standard boilerplate
              code I always use. I then access images and the like with $httpPath and
              text and config files with $filePath.

              --
              The email address used to post is a spam pit. Contact me at
              http://www.derekfountain.org : <a
              href="http://www.derekfounta in.org/">Derek Fountain</a>

              Comment

              • Ian Rastall

                #8
                Re: Dynamic Image SRC

                In comp.lang.php Erwin Moller wrote:
                [color=blue]
                > In that includefile I have 1 variable that contains the:
                > * full URL to root of your application.
                > (in the include:)
                > $myAppRoot = "http://localhost/gongfamily/";
                > Including that variable makes it easy to refer to the
                > imagedirectorie s, no matter from where you use them:
                > <img src="<?= $myAppRoot ?>/images/layout/version1/foo.gif">[/color]

                Well, I'm running into problems. I have at the top of each of my
                two (so far) index files:

                <?php $myAppRoot = "http://localhost/gongfamily/"; ?>

                Then for my include, I write:

                <?php
                include('$myApp Root."/sundry/header.txt"');
                ?>

                But it doesn't recognize it. I'm not sure if it's working with
                the images, either, as when I look at the source code for the
                main index file, the images show up as src="images/foo.gif"
                instead of as src="http://localhost/gongfamily/images/foo.gif".

                I'm sure this is a simple problem that a real PHP hacker would
                understand, but I'm befuddled.

                Ian
                --

                Comment

                • Erwin Moller

                  #9
                  Re: Dynamic Image SRC

                  Ian Rastall wrote:
                  [color=blue]
                  > In comp.lang.php Erwin Moller wrote:
                  >[color=green]
                  >> In that includefile I have 1 variable that contains the:
                  >> * full URL to root of your application.
                  >> (in the include:)
                  >> $myAppRoot = "http://localhost/gongfamily/";
                  >> Including that variable makes it easy to refer to the
                  >> imagedirectorie s, no matter from where you use them:
                  >> <img src="<?= $myAppRoot ?>/images/layout/version1/foo.gif">[/color]
                  >
                  > Well, I'm running into problems. I have at the top of each of my
                  > two (so far) index files:
                  >
                  > <?php $myAppRoot = "http://localhost/gongfamily/"; ?>
                  >
                  > Then for my include, I write:
                  >
                  > <?php
                  > include('$myApp Root."/sundry/header.txt"');
                  > ?>[/color]

                  which is wrong indeed. :P
                  You include a file on your LOCAL FILESYSTEM, not via http.

                  Two advises:
                  1) Do not put
                  <?php $myAppRoot = "http://localhost/gongfamily/"; ?>
                  above every script.
                  Make THAT an includefile, so you have to change this ONLY on one location
                  when you have 100 files.

                  2) Make 2 variables in that includescript:
                  a) One for your local path to your app
                  eg
                  $MyLocalRoot = "/home/ian/public_html/myapp/";

                  b) One for http-requests:
                  $myLocalHttpRoo t = "http://www.yoursite.co m/";


                  You mixed them us, and in your example you try to include an includefile
                  (php) via http.

                  Hope that helps.

                  Also, read the response of Derek too in this thread. :-)

                  Regards,
                  Erwin Moller

                  [color=blue]
                  >
                  > But it doesn't recognize it. I'm not sure if it's working with
                  > the images, either, as when I look at the source code for the
                  > main index file, the images show up as src="images/foo.gif"
                  > instead of as src="http://localhost/gongfamily/images/foo.gif".
                  >
                  > I'm sure this is a simple problem that a real PHP hacker would
                  > understand, but I'm befuddled.
                  >
                  > Ian[/color]

                  Comment

                  • Ian Rastall

                    #10
                    Re: Dynamic Image SRC

                    In comp.lang.php Erwin Moller wrote:
                    [color=blue]
                    > 2) Make 2 variables in that includescript:
                    > a) One for your local path to your app
                    > eg
                    > $MyLocalRoot = "/home/ian/public_html/myapp/";
                    >
                    > b) One for http-requests:
                    > $myLocalHttpRoo t = "http://www.yoursite.co m/";
                    >
                    >
                    > You mixed them us, and in your example you try to include an
                    > includefile (php) via http.
                    >[/color]

                    Thanks, Erwin. I appreciate the help.

                    Ian
                    --

                    Comment

                    • RootShell

                      #11
                      Re: Dynamic Image SRC

                      "Derek Fountain" <nospam@example .com> escreveu na mensagem
                      news:4210739c$0 $30059$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...[color=blue][color=green]
                      >> This works fine with the front page, but in the index page for
                      >> http://localhost/gongfamily/daevid/
                      >>
                      >> the images are broken, because the links are relative. I could
                      >> make the links absolute, but then I couldn't use my testing
                      >> server. There might be an Apache solution to this, such as
                      >> setting up some sort of alias or virtual root, but that stuff is
                      >> beyond me.[/color]
                      >
                      > My current project lives in a directory called 'dp', and my test machine
                      > is
                      > called 'monkey' (or 'localhost'). I use this function to set a couple of
                      > path variables:
                      >
                      > function setPaths( &$filePath, &$httpPath )
                      > {
                      > if( (strcmp( $_SERVER['SERVER_NAME'], "monkey" ) == 0) ||
                      > (strcmp( $_SERVER['SERVER_NAME'], "localhost" ) == 0) ) {
                      > $filePath = "/home/derek/public_html/dp/";
                      > $httpPath = "/~derek/dp/";
                      > } else {
                      > $filePath = "/home/derek/html/www.dp.com/public_html/";
                      > $httpPath = "/";
                      > }
                      > }
                      >
                      > This gets called at the top of each page as part of the standard
                      > boilerplate
                      > code I always use. I then access images and the like with $httpPath and
                      > text and config files with $filePath.
                      >
                      > --
                      > The email address used to post is a spam pit. Contact me at
                      > http://www.derekfountain.org : <a
                      > href="http://www.derekfounta in.org/">Derek Fountain</a>[/color]

                      I used something similar and it's pretty simple :)

                      Regards,
                      RootShell


                      Comment

                      Working...