我正在努力使用 PHP 在所有页面上为自己提供相同的页眉、导航和页脚元素,但我遇到了问题;出于某种原因,.php 文件无法识别 include("/header.html") 的 header.html 文件
.php文件
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<link rel="stylesheet" type="text/css" href="/style.css">
<title>Sample Game Type</title>
</head>
<body>
<?php include("/header.html"); ?>
<div id="gamelisting"><!--Gametype Shortcuts-->
<a href="http://www.solarfusionnerf.ca/game-types/carpe-testiculum.html">
<div class="gametype">'Carpe Testiculum' (also known as just Carpe)</div>
</a>
<div class="gametype">Other Game Here</div>
<div class="gametype">Add more as neccessary</div>
</div>
<div class="gameinfo"><!--Information about game type-->
<h2 style="text-align:center;"></h2><!--Name-->
<h3>Description</h3>
<h3>Objective</h3>
<h3>Rules</h3>
</div>
</body>
然后是我的 header.html 文件。
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Header</title>
</head>
<body>
<li><!--Header Buttons-->
<a href="http://www.solarfusionnerf.ca">
<div id="header">
Home
</div>
</a>
<a href="http://www.solarfusionnerf.ca/mods.html">
<div id="header">
Mods
</div>
</a>
<a href="http://www.solarfusionnerf.ca/selling.html">
<div id="header">
Selling
</div>
</a>
<a href="http://www.solarfusionnerf.ca/gallery.html">
<div id="header">
Gallery
</div>
</a>
<a href="http://www.solarfusionnerf.ca/contact.html">
<div id="header">
Contact
</div>
</a>
</li>
</body>
我做错了什么? header.html 文件位于我的 pubic_html 文件的根目录中,我试图连接的 .php 页面位于目录/game-types
最佳答案
当您使用 <?php include("/header.html"); ?>
时您实际上是在说在我的整个文件系统的根目录下寻找一个名为 header.html 的文件;不在包含所有其他文件的 public_html 目录(甚至在预定义的 include_path 中),例如/path/to/public_html/header.html,您实际上是在/header.html 中查找文件。
听起来你想包含一个相对于当前目录的文件。
像这样:
include(__DIR__ . '/header.html');
或
include('header.html');
关于PHP 在目录中找不到我的 header.html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33357644/