Web开发 - 没有重复文件的相对URL

时间:2011-01-14 11:42:51

标签: php include relative-path relative-url

我在根文件夹中有index.php的网站,/img中的图片和overview.php中的/content。我有一个sidebar.php文件,该文件包含在index.phpoverview.php中。如果我在每个文件中包含一个链接,我应该如何引用/img/image.gifimage.gif的位置相对于引用它的文件的位置发生了变化 在/img/image.gif中使用sidebar.php将在index.php中使用,但在位于/content/overview.php的文件中失败。
我能看到的唯一解决方案是在每个子目录中包含一个单独的sidebar.php,或在每个子目录中包含一个/img目录。
我能找到的最佳建议是使用<base&gt;这里建议的HTML标记:
Change relative link paths for included content in PHP

然而,在同一个链接中,SamGoody建议<base&gt;标记 is no longer properly supported in Internet Explorer, since version 7.

在采取行动之前,我想对此事有所了解。

感谢。

编辑:我使用"../"

下面的错误方法

示例 -

根/ 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'); ?>
...  

根/包括/ header.php文件:

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

根/内容/ 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'); ?>
...

1 个答案:

答案 0 :(得分:3)

  

在sidebar.php中使用/img/image.gif将在index.php中运行,但对于位于/content/overview.php的文件失败

但它不应该。前面的/使它成为绝对路径,可以在服务器上的任何位置工作。如果这对你不起作用,那么就会出现问题 - 在这种情况下,发布一些例子。

除非您计划有一天将整个站点移动到子目录中,或者将图像移动到Content Delivery Network(这两个操作都需要重写地址),否则您可以安全地使用绝对URL。