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?
image.gif
changes relative to th开发者_StackOverflowe 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'); ?>
...
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.
精彩评论