473,396 Members | 1,996 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,396 software developers and data experts.

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
Jun 18 '06 #1
19 5043
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joe Cybernet | last post by:
Is there any function for combining an absolute and a relative URL to result in an absolute URL? Like if I have http://www.domain.com and "../images/1.jpeg" it will evaluate to...
3
by: Peter Taurins | last post by:
Hi there. I have an included file (header.php) that contanis a reference to a graphic. If I stay at the root level, then I can control the relative path of the image. eg. images/imagename.jpg ...
15
by: Nick K. | last post by:
I recently began maintenance work on a production web server that is located in the root directory of a web server. I moved this into a sub web on my local web server in order to do work on it. I...
4
by: Richard | last post by:
We are distributing a VS2003 solution to our customers that includes a .NET assembly (dll file) and a sample project for how to use the dll. The customer can then customize the sample or add a new...
7
by: Rizaan Jappie | last post by:
is it possible to get the relative path based on a absolute path in c#? *** Sent via Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
6
by: openleren | last post by:
Hi all, how can I use a relative path in my web.config file for an Access db?: Instead of using <configuration> <appSettings> <add key="conAccess" value="microsoft.jet.oledb.4.0;data...
8
by: Daniel Serrano | last post by:
Hi, is it possible to use a relative path in a Connection string to access an Access database in a stand alone visual basic desktop application. All the examples that I have seen have absolute...
8
by: JJ | last post by:
I'm confused about paths. I have a functionn that uses the mappath method, which I think requires a virtual path (is that the same as a relative path?). But this doesn't always work as the...
15
by: Lars Eighner | last post by:
Aside from the deaths of a few extra electrons to spell out the whole root relative path, is there any down side? It seems to me that theoretically it shouldn't make any difference, and it would...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.