Dynamic Image SRC

This is a discussion on Dynamic Image SRC within the PHP Language forums, part of the PHP Programming Forums category; I would like to use an include for my header and navigation, and because subsequent files may be in different ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-14-2005
Ian Rastall
 
Posts: n/a
Default 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
--
http://www.bookstacks.org/
Reply With Quote
  #2 (permalink)  
Old 02-14-2005
Erwin Moller
 
Posts: n/a
Default Re: Dynamic Image SRC

Ian Rastall wrote:

> 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



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
Reply With Quote
  #3 (permalink)  
Old 02-14-2005
Ian Rastall
 
Posts: n/a
Default Re: Dynamic Image SRC

In comp.lang.php Erwin Moller wrote:

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


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
--
http://www.bookstacks.org/
Reply With Quote
  #4 (permalink)  
Old 02-14-2005
Erwin Moller
 
Posts: n/a
Default Re: Dynamic Image SRC

Ian Rastall wrote:

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

>
> 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



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 imagedirectories, 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
Reply With Quote
  #5 (permalink)  
Old 02-14-2005
Ian Rastall
 
Posts: n/a
Default Re: Dynamic Image SRC

In comp.lang.php Erwin Moller wrote:

> 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
> imagedirectories, 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.


Thank you! That's a good solution. Not entirely hands-off, but
very easy to modify.

> 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.


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
--
http://www.bookstacks.org/
Reply With Quote
  #6 (permalink)  
Old 02-14-2005
Erwin Moller
 
Posts: n/a
Default Re: Dynamic Image SRC

Erwin Moller wrote:

I wasn't very clear. Some modifications:

>
> 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)


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 "$fullPathToRoot/include/somefunctions.php";

So the same principle, but not for a http:// but for a normal path.

> 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 imagedirectories, 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.


Try:

<pre>
<? var_dump($_SERVER); ?>
</pre>

to see the content.

check: http://nl2.php.net/reserved.variables

>
> Good luck.
>
> Regards,
> Erwin Moller


Reply With Quote
  #7 (permalink)  
Old 02-14-2005
Derek Fountain
 
Posts: n/a
Default Re: Dynamic Image SRC

> 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.


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.derekfountain.org/">Derek Fountain</a>
Reply With Quote
  #8 (permalink)  
Old 02-14-2005
Ian Rastall
 
Posts: n/a
Default Re: Dynamic Image SRC

In comp.lang.php Erwin Moller wrote:

> 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
> imagedirectories, no matter from where you use them:
> <img src="<?= $myAppRoot ?>/images/layout/version1/foo.gif">


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('$myAppRoot."/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
--
http://www.bookstacks.org/
Reply With Quote
  #9 (permalink)  
Old 02-14-2005
Erwin Moller
 
Posts: n/a
Default Re: Dynamic Image SRC

Ian Rastall wrote:

> In comp.lang.php Erwin Moller wrote:
>
>> 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
>> imagedirectories, no matter from where you use them:
>> <img src="<?= $myAppRoot ?>/images/layout/version1/foo.gif">

>
> 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('$myAppRoot."/sundry/header.txt"');
> ?>


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:
$myLocalHttpRoot = "http://www.yoursite.com/";


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


>
> 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


Reply With Quote
  #10 (permalink)  
Old 02-14-2005
Ian Rastall
 
Posts: n/a
Default Re: Dynamic Image SRC

In comp.lang.php Erwin Moller wrote:

> 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:
> $myLocalHttpRoot = "http://www.yoursite.com/";
>
>
> You mixed them us, and in your example you try to include an
> includefile (php) via http.
>


Thanks, Erwin. I appreciate the help.

Ian
--
http://www.bookstacks.org/
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:19 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0