[英]How do I tell my PHP Include to source images from my root?
Let me re-explain my problem. 让我重新解释一下我的问题。
My header is stored in header.php
, I'm using <?php include('/header.php');?>
to grab it. 我的标题存储在
header.php
,我正在使用<?php include('/header.php');?>
来抓取它。 This works fine on all web pages in my root. 这适用于我的根目录中的所有网页。
But, I have a folder called Catalogue in my root with more web pages, so then I change it to <?php include('../header.php'); ?>
但是,我的根目录中有一个名为Catalog的文件夹,包含更多网页,因此我将其更改为
<?php include('../header.php'); ?>
<?php include('../header.php'); ?>
which again works fine. <?php include('../header.php'); ?>
再次正常工作。
however, I am sourcing a few images in my header.php
. 但是,我在
header.php
了一些图像。 The images do not appear on web pages in my catalogue folder, because I am sourcing them from the images folder in my root <img src="images/cosworth2.jpg">
图像不会出现在我的目录文件夹中的网页上,因为我从根目录中的images文件夹中获取图像
<img src="images/cosworth2.jpg">
I can't use <img src="../images/cosworth2.jpg"
because this will then affect the images that work fine on the web pages that aren't in my catalogue folder. 我无法使用
<img src="../images/cosworth2.jpg"
因为这会影响在我的目录文件夹中不在的网页上正常工作的图像。
Does this make sense? 这有意义吗?
I need to tell my header.php
to source all of my images from the root even if my web pages are in other directories. 我需要告诉我的
header.php
从根目录中获取所有图像,即使我的网页在其他目录中也是如此。
Header.php header.php文件
<div id="top"><center><img src="images/header.png" style="max-width:100%;"></center></div>
<header id="header" class="site-header" role="banner">
<div id="header-inner" class="container sixteen columns over">
<hgroup class="one-third column alpha">
</hgroup>
<nav id="main-nav" class="two thirds column omega">
<ul>
<li>
<a href="index.php">Home</a>
</li>
<li>
<a href="about-us.php">About Us</a>
</li>
<li>
<a href="news.php">News</a>
</li>
<li>
<a href="dealers.php">Dealers</a>
</li>
<li>
<a href="products.php">Products</a>
</li>
<li>
<a href="http://www.cosworth-europe.co.uk/shop">Buy Online</a>
</li>
<li>
<a href="contactus.php">Contact</a>
</li>
</ul>
</nav>
</div>
<div class="container"><img src="images/cosworth2.jpg" style="max-width:100%; height:auto; float:left; margin-right:20px; margin-top:20px;">
<p style="padding-top:25px;">Welcome to <strong>Cosworth Europe</strong>. This website is specifically for Cosworth Aftermarket & Performance Products. For any other enquires please visit Cosworth.com. For more information, feel free to visit the About Us page.</p></div>
</header>
Use an absolute path. 使用绝对路径。
<img src="/images/..." alt="...">
Describe the location of your images (and stylesheets, etc) relative to the root of the website. 描述图像(和样式表等)相对于网站根目录的位置。
To add to others answers, you could define()
a PHP Constant for the purpose of setting the absolute path. 要添加其他答案,您可以
define()
PHP常量以设置绝对路径。 This would help if you ever had to change the directory/path. 如果您不得不更改目录/路径,这将有所帮助。
//perhaps add this in a config file or at the top of the header.php file
define( 'IMAGE_PATH', '/images' );
//use in any other file
<img src="<?php echo IMAGE_PATH; ?>/image1.png" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.