我有一个网站,根文件夹中包含 index.php
,图像位于 /img
中,overview.php
位于 /content< /代码>。我有一个 sidebar.php
文件,它包含在 index.php
和 overview.php
中。如果我在每个文件中包含链接,我应该如何引用 /img/image.gif
?
image.gif
的位置相对于引用它的文件的位置发生变化。
在 sidebar.php
中使用 /img/image.gif
将在 index.php
中工作,但对于位于 的文件则失败/content/overview.php
.
我能看到的唯一解决方案是在每个子目录中包含一个单独的 sidebar.php
,或者在每个子目录中包含一个 /img
目录。
我能找到的最好的建议是使用 >这里建议的 HTML 标签:
更改 PHP 中包含内容的相对链接路径< /a>
然而,在同一个链接中,SamGoody 建议 > 自版本 7 起,Internet Explorer 不再正确支持 标记。
在采取行动之前,我希望对此事有一些了解。
谢谢。
编辑:我使用下面的错误方法 "../"
示例 -
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'); ?>
...
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'); ?>
...
发布评论
评论(1)
但它不应该。前面的
/
使其成为绝对路径,可以从服务器上的任何位置工作。如果这对您不起作用,则说明某处存在问题 - 在这种情况下,请发布一些示例。除非您计划有一天将整个站点移动到子目录中,或将图像移动到内容交付网络(这两种操作都需要重写地址),否则您可以安全地使用绝对 URL。
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.