1

I have encountered a PATH problem when building a small web site. The site consists of a few standard pages in different folders and a few others in a folder named _include. I now have three files included on each page - menu.php, header.php and footer.php - all three contained in the folder _include. I have used set_include_path and it works quite well as far as PHP goes. However, there are problems when it comes to HTML. In the header.php file I have this syntax <img src="_pics/header.png" width="XXX" height="YYY" />

The code works fine and the picture loads as long as all the files that includes header.php are in the same directory. But if I try to include it in a page in a subdir, then the picture doesn't load, which is pretty obvious, seeing as the directory level now has changed.

So my question is how can I solve this issue so that HTML-paths work in included PHP-files, regardless of where they are placed on the site?

Sandokan
  • 861
  • 1
  • 9
  • 18

4 Answers4

4

The easiest solution is to always use absolute paths for assets relative to the web-root:

<img src="/_pics/header.png" width="XXX" height="YYY" />
          ^ absolute path

That applies to all stuff like images, javascript files, css files, etc.

Now it does not matter where your include file is located, that could even be outside of the web-root.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Doesn't change a thing. It still tries to find the picture relative to the path I am in. If I am in root level it works, but if I go down one level it can't find the picture because it now tries to find it in "subdir/_pics" instead. – Sandokan Nov 22 '12 at 14:27
  • @Sandokan No, when php generates html with a path like this, the browser tries to locate `/_pics/header.png` from your web-server as soon as it has loaded the html. The location of your include and the location of the image are not related in any way. – jeroen Nov 22 '12 at 14:32
  • Well, whatever the problem is, it still doesn't work. Same result. Pages in root dir load the image fine. Pages in any other folder dont - they give a 404 error. – Sandokan Nov 22 '12 at 14:39
  • @Sandokan Can you add the html of one of the pages that does not work and the complete url of an image that does not load in that html to your question? – jeroen Nov 22 '12 at 14:46
  • 1
    *This is the correct answer.* If you do that it DEFINITELY WILL NOT try to find the image relative to the location of the file. – Iain Collins Nov 22 '12 at 14:55
  • 192.168.237.129/Blixten/fkblixten_v0.1/public_html/_pics/header.png is the URL loaded from a working page in the root. From a page in SUBDIR1 i get the following: 192.168.237.129/Blixten/fkblixten_v0.1/public_html/SUBDIR1/_pics/header.png was not found on this server. So I'm really not sure what to believe now. – Sandokan Nov 22 '12 at 15:01
  • @Sandokan You need to post a section of the html with the image and the complete url to the image. – jeroen Nov 22 '12 at 15:04
  • The html code is right there in my OP but now I have added a / as you said. The complete URL to the image is http://192.168.237.129/Blixten/fkblixten_v0.1/public_html/_pics/header.png. Could it have anything to do with the fact that I have a defined alias in Apache? – Sandokan Nov 22 '12 at 15:08
  • @Sandokan Could be, but it is impossible to see what is happening as you are on a local network. – jeroen Nov 22 '12 at 15:47
  • Gonna check it out when I have time. Thanks for your time. – Sandokan Nov 22 '12 at 16:55
  • Still no luck. When checking page info the file header.png it's the only image with a incorrect path. As it shows now it's http:///_pics/header.png, whereas the WORKING images all have the path http:///public_html/_pics/image.jpg. It's seems like it searches for that particular image one level too high. – Sandokan Nov 23 '12 at 08:47
  • @Sandokan The problem seems to be the configuration of your web-server: Normally a path ending in `public_html/` would be your web-root but it seems that you have configured the web-root to be one directory higher. You can either change the web-root (I'd do that..) or use `/public_html/_pics/` as an absolute path to your images. – jeroen Nov 23 '12 at 14:01
  • Spot on. I did some changes in my web server and it worked like a charm Thanks for your time. – Sandokan Nov 24 '12 at 13:31
0

If your paths are static you can use the php document_root or server_name variables.

<?php echo "<img src=\"http://".$_SERVER['SERVER_NAME']."/_pics/header.png\" width="XXX" height="YYY" />"; ?>
<?php echo "<img src=\"".$_SERVER['DOCUMENT_ROOT']."/_pics/header.png\" width="XXX" height="YYY" />"; ?>

If your pages get moved though you will need to change it in this code to reflect the new location.

Laywah
  • 94
  • 1
  • 11
0

You must understand the difference between an absolute and relative URL.

The code works fine and the picture loads as long as all the files that includes header.php are in the same directory. But if I try to include it in a page in a subdir, then the picture doesn't load, which is pretty obvious, seeing as the directory level now has changed.

<img src="_pics/header.png" width="XXX" height="YYY" />

How it maps when you try to include in a sub directory-

<img src="subdir/_pics/header.png" width="XXX" height="YYY" />

As you know the picture is not placed under subdir hence the picture is not loaded.

In this case you must use absolute URL which starts withhttp:// and ends wtih picname.png for an example.

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
-1

Use full path eg: http:// domain_name/path .This is possibly what you are looking.

harikrishnan.n0077
  • 1,927
  • 4
  • 21
  • 27