Help - Search - Member List - Calendar
Full Version: Relative path & php
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
AF
Help.

Every time I use php my relative path gets messed up.

For example take this code in an html file: (
I have abbreviated it for illustration purposes)

<img src="library_files/logo.gif" alt="Mortgage Loans" border="0"
width="128" height="122"></a><br><br></td>
<a href="index.html"><font size="1">Home</font></a></p>

works great.

I then put it into any php file and it doesn't.

I have to change it to this in order to get it to work.

<img src="http://www.domainname.com/library_files/logo.gif"
alt="Mortgage Loans" border="0" width="128"
height="122"></a><br><br></td>
<a href="http://www.domainname.com/index.html"><font
size="1">Home</font></a></p>

Does anyone know why this is happening and how to fix the problem?

When I say fix, I mean set the path programmatically so that I do not
have to go manually edit every file and add http://www.domainname.com/
to every href and src tag.

Thanks for any help.

Derick van Niekerk
AF wrote:
QUOTE
Help.

Every time I use php my relative path gets messed up.

For example take this code in an html file: (
I have abbreviated it for illustration purposes)

<img src="library_files/logo.gif" alt="Mortgage Loans" border="0"
width="128" height="122"></a><br><br></td
<a href="index.html"><font size="1">Home</font></a></p

works great.

I then put it into any php file and it doesn't.

I have to change it to this in order to get it to work.

<img src="http://www.domainname.com/library_files/logo.gif"
alt="Mortgage Loans" border="0" width="128"
height="122"></a><br><br></td
<a href="http://www.domainname.com/index.html"><font
size="1">Home</font></a></p

Does anyone know why this is happening and how to fix the problem?

When I say fix, I mean set the path programmatically so that I do not
have to go manually edit every file and add http://www.domainname.com/
to every href and src tag.

Thanks for any help.

Try prefixing with './' - as in:

<img src="./library_files/logo.gif" alt="Mortgage Loans" border="0"
width="128" height="122"></a><br><br></td>
<a href="index.html">

Hilarion
AF wrote:
QUOTE
Every time I use php my relative path gets messed up.

For example take this code in an html file: (
I have abbreviated it for illustration purposes)

<img src="library_files/logo.gif" alt="Mortgage Loans" border="0"
width="128" height="122"></a><br><br></td
<a href="index.html"><font size="1">Home</font></a></p

works great.

I then put it into any php file and it doesn't.

I have to change it to this in order to get it to work.

<img src="http://www.domainname.com/library_files/logo.gif"
alt="Mortgage Loans" border="0" width="128"
height="122"></a><br><br></td
<a href="http://www.domainname.com/index.html"><font
size="1">Home</font></a></p

Does anyone know why this is happening and how to fix the problem?

When I say fix, I mean set the path programmatically so that I do not
have to go manually edit every file and add http://www.domainname.com/
to every href and src tag.


It's probably not caused by PHP but by the way you include files.
Assume we have:

PHP file "top.inc" located in root folder containing:
<div>
This is top of page with some image: <img src="some_image.gif" />
</div>

File "some_image.gif" is also located in root folder.
In the same (root) folder we have also "index.php" file containing:
<html>
<body>
<?php
include( 'top.inc' );
?>
<div>
This is a main page.
</div>
</body>
</html>

This file will display the image properly. But if we also have
subfolder called "sub" containing "index.php" file with:
<html>
<body>
<?php
include( '../top.inc' );
?>
<div>
This is a subpage.
</div>
</body>
</html>

Then the "sub/index.php" will not display the image properly
cause image tag contains relative path to "some_image.gif"
which is now relative to "sub" folder (from which we called
"index.php"). The fact that "top.inc" file is in root folder
is irrelevant here.

You'll have similar problem if the "top.inc" and "some_image.gif"
were located in "sub" folder but included in PHP file called
from root folder.

When you use relative paths in PHP, you have to consider
where your files are located and where from they are included.
All relative URLs (<a href>, <img src>, <script src> etc.) given
in output HTML will be considered relative to the path of file
which was initially called (the browser has no idea that
some URLs were generated by files included from subfolders).


Another cause could be wrapping URL parameters as fragments
of path, eg.:
http://some.domain.com/some_path/some_scri...p/param1/param2
In this case "some_script.php" in "some_path" is generating
output HTML, but all URLs will be relative to virtual path
http://some.domain.com/some_path/some_scri...p/param1/param2



In the example above the easiest solution would be to change
relative path to the image file to server absolute path
(you do not have to provide Internet absolute path, which
means including protocol name and server name):
<img src="/some_image.gif" />
or (in the second case):
<img src="/sub/some_image.gif" />
It'll also work in case of path-wrapped parameters.


Hilarion

Tony
Hilarion wrote:
QUOTE
It's probably not caused by PHP but by the way you include files.
Assume we have:

PHP file "top.inc" located in root folder containing:
<div
This is top of page with some image: <img src="some_image.gif" /
</div

File "some_image.gif" is also located in root folder.
In the same (root) folder we have also "index.php" file containing:
<html
<body
<?php
include( 'top.inc' );

<div
This is a main page.
</div
</body
</html

This file will display the image properly. But if we also have
subfolder called "sub" containing "index.php" file with:
<html
<body
<?php
include( '../top.inc' );

<div
This is a subpage.
</div
</body
</html

Then the "sub/index.php" will not display the image properly
cause image tag contains relative path to "some_image.gif"
which is now relative to "sub" folder (from which we called
"index.php"). The fact that "top.inc" file is in root folder
is irrelevant here.

Umm - are you talking about indluded files, or directly accessing them?. The
relative path should be from where the ROOT PHP document is.

(Ah - missed it earlier - you said that later in your post!)

As an example, for clarification, I have the following file structure:

/ - contains the files "index.php" and "style.css"
/images - contains all image files
/includes - contains "header.php" and "footer.php"
/pages - contains "page.php"

If /pages/page.php is INCLUDED by "index.php" I need to use <img
src="images/image.jpg"> to display the image. That's because the root file
that INCLUDES pages/page.php is in the "/" directory, which is the base
directory for the HTML.

Now, if I were to open "pages/page.php" directly, the images wouldn't show
up, since the root PHP document is now in the sub-folder.

QUOTE
In the example above the easiest solution would be to change
relative path to the image file to server absolute path

Probably makes sense in this case - I would agree :)

--
Tony Garcia
Web Right! Development
Riverside, CA
www.WebRightDevelopment.com

AF
On Thu, 7 Jul 2005 19:40:31 +0200, "Hilarion"
<[Email Removed]> wrote:

snip

QUOTE
Another cause could be wrapping URL parameters as fragments
of path, eg.:
http://some.domain.com/some_path/some_scri...p/param1/param2
In this case "some_script.php" in "some_path" is generating
output HTML, but all URLs will be relative to virtual path
http://some.domain.com/some_path/some_scri...p/param1/param2



In the example above the easiest solution would be to change
relative path to the image file to server absolute path
(you do not have to provide Internet absolute path, which
means including protocol name and server name):
<img src="/some_image.gif" /
or (in the second case):
<img src="/sub/some_image.gif" /
It'll also work in case of path-wrapped parameters.


Hilarion

Thanks for the post.

I am using the format you mention of:

QUOTE
http://some.domain.com/some_path/some_script.php/param1/param2

I think I have tried your suggestion, although I am not sure I follow
you paragraph above. I have taken my code shown below

<img src="library_files/logo.gif" alt="Mortgage Loans" border="0"
width="128" height="122"></a><br><br></td>
<a href="index.html"><font size="1">Home</font></a></p>

and added a ../ so that it now read

<img src="../library_files/logo.gif" alt="Mortgage Loans"
border="0"
width="128" height="122"></a><br><br></td>
<a href="index.html"><font size="1">Home</font></a></p>

library_files is a directory right under the root dir, and index.html
is in the root.

I am still not sure what you mean though. Could you take the code I
show above and change the root to how you think it will work?

I am lost on what is the server absolute path. And depending on what
you mean is there a way to change the directory paths programatically
without out having to rewrite my original code?

PS I know how to fill in a php variable and pt the variable in front
to create a correct path, as in

<?

$URL = "http://www.domain.com/";

?>

<img src="<? echo $URL; ?>library_files/logo.gif" alt="Mortgage
Loans" border="0"
width="128" height="122"></a><br><br></td>
<a href="<? echo $URL; ?>index.html"><font
size="1">Home</font></a></p>

I jsut would prefer to change or set the path at the beginning of a
file rather than have to add the code to every file and link.

Again thanks for all of the post.

Hilarion
QUOTE
I think I have tried your suggestion, although I am not sure I follow
you paragraph above.  I have taken my code shown below

<img src="library_files/logo.gif" alt="Mortgage Loans" border="0"
width="128" height="122"></a><br><br></td
<a href="index.html"><font size="1">Home</font></a></p

and added a ../ so that it now read

<img src="../library_files/logo.gif" alt="Mortgage Loans"
border="0"
width="128" height="122"></a><br><br></td
<a href="index.html"><font size="1">Home</font></a></p

library_files is a directory right under the root dir, and index.html
is in the root.


If you use "../", then it's still relative path.
If you are using parameter wrapping, then you should use
absolute path (server absolute or Internet absolute):

<img src="/library_files/logo.gif" alt="Mortgage Loans"
border="0"
width="128" height="122"></a><br><br></td>
<a href="/index.html"><font size="1">Home</font></a></p>

Notice that I used absolute path also for HREF.


QUOTE
I am lost on what is the server absolute path.

"index.html", "../dir/some_file.ext", "some_path/another.file" are
relative paths and they are evaluated (when appear in HTML output)
by web browser relatively to current document path (warning:
if current document path ends with "/" sign, then it's treated
as a folder, but if it's not, then last part is treated as file
name and is stripped from path).
If curren document path is:
http://some.domain.com/some_dir/some_doc.html
then above paths lead to:
http://some.domain.com/some_dir/index.html
http://some.domain.com/dir/some_file.ext
http://some.domain.com/some_dir/some_path/another.file
If current path is:
http://some.domain.com/another_dir/another...p/param1/param2
then above paths lead to:
http://some.domain.com/another_dir/another...ram1/index.html
http://some.domain.com/another_dir/another...r/some_file.ext
http://some.domain.com/another_dir/another...th/another.file
If current path is:
http://some.domain.com/another_dir/another.../param1/param2/
then above paths lead to:
http://some.domain.com/another_dir/another...ram2/index.html
http://some.domain.com/another_dir/another...r/some_file.ext
http://some.domain.com/another_dir/another...th/another.file

If you are using paths with "../" inside them, then remember that it's
evaluated on browser side, so you can't navigate this way above the
webserver root.

Absolute paths are:
- server absolute path - starts with "/" sign, which means that all
given paths are relative to webserver root folder (which may be and
in most cases is different than server filesystem root),
- internet absolute path - starts with "protocol" prefix eg. "http://",
"mailto:" etc. and gives absolute and complete URL to resource.


QUOTE
And depending on what
you mean is there a way to change the directory paths programatically
without out having to rewrite my original code?

PS I know how to fill in a php variable and pt the variable in front
to create a correct path, as in

<?

$URL = "http://www.domain.com/";

?

<img src="<? echo $URL; ?>library_files/logo.gif" alt="Mortgage
Loans" border="0"
width="128" height="122"></a><br><br></td
<a href="<? echo $URL; ?>index.html"><font
size="1">Home</font></a></p

I jsut would prefer to change or set the path at the beginning of a
file rather than have to add the code to every file and link.

The best way would probably be to change all your relative URL paths
to server absolute paths, but this requires rewriting most of your
PHP / HTML code.

You have other options which are not recomended but will work.
They are based on <base> tag (inside <head> tag of <html> tag).
If your <head> section is allready placed in one PHP/HTML file which is
included by all other files, then using <base> tag would require only
change in this one file. If not, then you'll have to change each
file which contains (or should contain) <head> section.
If you have http://some.domain.com/some_path/some_script.php document
which is called by http://some.domain.com/some_path/some_scri...p/param1/param2
URL and want all relative paths inside this document be calculated
basing on http://some.domain.com/some_path/some_script.php path, then
you should place this path in <base> tag like this:
<html>
<head>
<base href="http://some.domain.com/some_path/some_script.php" />
...
</head>
<body>
...
</body>
</html>
If <head> section is defined in one file which is included by other PHP
files which are located in different paths, then the way you should use
<base> tag in it depends on the way you use relative paths in your
documents. If the relative paths are relative to one specific folder
(same for all documents), then you should use <base> as above, but
if the paths are relative to the folder which contains the called
document, then base tag should be dynamically filled with path to
called document like this:
<html>
<head>
<base href="http://some.domain.com<?php echo htmlspecialchars( $_SERVER[ 'SCRIPT_NAME' ] ); ?>" />
...
</head>
<body>
...
</body>
</html>


Hilarion

AF
On Fri, 8 Jul 2005 15:26:18 +0200, "Hilarion"
<[Email Removed]> wrote:

snip

QUOTE
The best way would probably be to change all your relative URL paths
to server absolute paths, but this requires rewriting most of your
PHP / HTML code.
snip
Hilarion

Thanks for the post and all of the info.

Looks like I need to do some rewriting, as you point out above.

I will play with some of the other ideas and techniques you have
graciously written about. I think I understand the variuos path's now
and how they work.

Even if I can't do what I want to, I have learned a lot from
everyone's post on this topic.

Thanks again.


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.