473,320 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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/
Jul 17 '05 #1
10 3201
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
Jul 17 '05 #2
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/
Jul 17 '05 #3
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
Jul 17 '05 #4
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/
Jul 17 '05 #5
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


Jul 17 '05 #6
> 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>
Jul 17 '05 #7
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/
Jul 17 '05 #8
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


Jul 17 '05 #9
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/
Jul 17 '05 #10
"Derek Fountain" <no****@example.com> escreveu na mensagem
news:42***********************@per-qv1-newsreader-01.iinet.net.au...
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>


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

Regards,
RootShell
Jul 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sam | last post by:
Hello everyone, I have a table, which contains a picture column, I put URL info into it. "www.myweb.com/1.jpg..." I want to show this picture in my crystal report, I find some samples show the...
0
by: Michael Huhn | last post by:
I have a web page default.aspx and a dynamic image image.aspx. Image gets parameters via querystring and outputs a gif. Using JavaScript I reload the image with changed parameters quite often....
5
by: Tompa | last post by:
Hi, I would like to create images on the fly as a response to an http request. I can do this with PIL like this (file create_gif.py): from PIL import Image, ImageDraw print 'Status: 200 OK'...
3
by: JOSEPHINE ALVAREZ | last post by:
I have this code that I want to use to do a rollover. However, because the company I am doing it for is continually changing the text, I want to be able to use dynamic text to change the text on...
0
by: UtilityWarrior | last post by:
If you use Visual Basic 6 or VB.net and want to create PDFs from images royalty free then this DLL is for you. The Image to PDF Dynamic Link Library (DLL) will convert one or more images (JPEG,...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
9
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I...
7
by: dino d. | last post by:
Hi- I want to create a dynamic image with areas so that when the user clicks different areas, the user jumps to those pages. The problem is, I can't seem to figure out how to do this efficiently....
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.