0

I have a site with index.php in the root folder, images in /img , and overview.php in /content. I have a sidebar.php file that is included in both index.php and overview.php. How should I refer to /img/image.gif if I include a link in each file?
The location of image.gif changes relative to the location of the file that references it.
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php.
The only solution that I can see is to either include a separate sidebar.php in each sub-directory, or include an /img directory in every sub-directory.
The best suggestion that I can find is to use the <base> HTML tag as suggested here:
Change relative link paths for included content in PHP

However, in the same link, SamGoody suggests that the <base> tag is no longer properly supported in Internet Explorer, since version 7.

I'd like some insight on the matter before committing to a course of action.

Thanks.

EDIT: I am using the wrong approach below with "../"

Example-

root/index.php:

...  
<link rel="stylesheet" type="text/css" href="style.css" />  
<title>title</title>  
</head>  
<body>  
<?php include('include/header.php'); ?>  
<?php include('include/menu.php'); ?>
...  

root/include/header.php:

...  
<div id="header">  
<span class="fl"><img src="img/dun1.png"/></span><span class="fr"><img src="img/dun2.png"/></span> 
...  

root/content/overview.php:

...  
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />  
<title>Overview</title>  
</head>  
<body>  
<?php include('../include/header.php'); ?>
<?php include('../include/menu.php'); ?>
...
Community
  • 1
  • 1
eshriek
  • 3
  • 2

1 Answers1

3

Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php

But it shouldn't. The preceding / makes it an absolute path which will work from any point on the server. If this doesn't work for you, there's a problem somewhere - in that case, post some examples.

Unless you are planning to move the whole site into a sub-directory one day, or move images to a Content Delivery Network (both actions would require re-writing the addresses) you can safely use absolute URLs.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • thanks, that's helped clarify some things for me. I've tried / and ../ , but cannot get it to work. If I change "style.css" to "/root/style.css" in overview.php I do get the style, but I cannot apply the same idea to images. Using "/root/" is probably a poor choice for this example as it is not the root of my localhost. The "/root" I refer to is located at "var/www/root" I'm guessing it's something I don't know about relative/absolute paths, or something I don't know about php includes, or my apache server is misconfigured, or all of the above. – eshriek Jan 14 '11 at 15:37
  • @eshriek what is your Apache web root? `www` or `root`? – Pekka Jan 14 '11 at 15:38
  • I think I have it figured out. I've moved everything to apache web root (www). In "[www]/index.php" I have "style.css" and "include/header.php" whereas in "[www]/content/overview.php" I use "/style.css" and "../include/header.php". I don't think this is ideal, but it works. I'm just afraid I may run into problems when I upload it to the actual server. – eshriek Jan 14 '11 at 15:58