By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,002 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

relative path / absolute path

P: n/a
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
Jun 18 '06 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Jerry M. Gartner wrote:
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.

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-

Jun 18 '06 #2

P: n/a
Jerry M. Gartner wrote:
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.

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

Jun 18 '06 #3

P: n/a
On Sun, 18 Jun 2006 10:24:20 GMT Jerry M. Gartner wrote:
[...] 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.


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.jpg"/>

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ä
Jun 18 '06 #4

P: n/a
Jan Thomä wrote:
On Sun, 18 Jun 2006 10:24:20 GMT Jerry M. Gartner wrote:
[...] 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.


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

<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_prefix="http://my.server.com/~colin/project/images/";
$image_file_prefix="/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.
Jun 18 '06 #5

P: n/a
Jerry M. Gartner wrote:
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.


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.
js*******@attglobal.net
==================
Jun 18 '06 #6

P: n/a
js*******@attglobal.net says...
Jerry M. Gartner wrote:

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.


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.


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

GM
Jun 18 '06 #7

P: n/a
Message-ID: <rr******************************@comcast.com> from Jerry
Stuckle contained the following:
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.


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/
Jun 18 '06 #8

P: n/a
Geoff Berrow wrote:
Message-ID: <rr******************************@comcast.com> from Jerry
Stuckle contained the following:

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.

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


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.
js*******@attglobal.net
==================
Jun 19 '06 #9

P: n/a
Geoff Berrow wrote:
Message-ID: <rr******************************@comcast.com> from Jerry
Stuckle contained the following:
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.


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


Aah, easily "fixed": DocumentRoot "/"

:)

--
/Bent
Jun 19 '06 #10

P: n/a
Jan Thomä wrote:
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/";


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

Jun 19 '06 #11

P: n/a
Geoff Berrow wrote:
Message-ID: <rr******************************@comcast.com> from Jerry
Stuckle contained the following:
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.


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


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/

Jun 19 '06 #12

P: n/a
On Sun, 18 Jun 2006 20:18:19 -0400, Jerry Stuckle wrote:
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.


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

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.


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

Jun 19 '06 #13

P: n/a
R. Rajesh Jeba Anbiah wrote:
Geoff Berrow wrote:
Message-ID: <rr******************************@comcast.com> from Jerry
Stuckle contained the following:

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.


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

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/


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

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 19 '06 #14

P: n/a
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" <js*******@attglobal.net> wrote in message
news:rr******************************@comcast.com. ..
Jerry M. Gartner wrote:
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.


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.
js*******@attglobal.net
==================

Jun 19 '06 #15

P: n/a
Jerry M. Gartner wrote:
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.


So, what's your PHP question? The only one you asked was about the <img> tag -
which is HTML, not PHP.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 20 '06 #16

P: n/a
Jerry Stuckle wrote:
<snip>
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.


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


So, instead of "images/..", if we use "/images/.." will it
validate?

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

Jun 20 '06 #17

P: n/a
R. Rajesh Jeba Anbiah wrote:
Jerry Stuckle wrote:
<snip>
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.


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

So, instead of "images/..", if we use "/images/.." will it
validate?

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


The problem is:

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

This is valid for xml but not html.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 20 '06 #18

P: n/a
Jerry Stuckle wrote:
R. Rajesh Jeba Anbiah wrote:

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

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


So, instead of "images/..", if we use "/images/.." will it
validate?


The problem is:

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

This is valid for xml but not html.


LOL.

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

Jun 25 '06 #19

P: n/a
R. Rajesh Jeba Anbiah wrote:
Jerry Stuckle wrote:
R. Rajesh Jeba Anbiah wrote:


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

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

So, instead of "images/..", if we use "/images/.." will it
validate?


The problem is:

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

This is valid for xml but not html.

LOL.

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


Try running it through an HTML validator. Or show me exactly where in the HTML
specs it says this is valid.

You can't - because it's not valid.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 25 '06 #20

This discussion thread is closed

Replies have been disabled for this discussion.