PHP 在目录中找不到我的 header.html 文件

标签 php html css

我正在努力使用 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/

相关文章:

php - 如何从 URL 下载文件而不显示 Laravel 中的完整路径?

css 页脚内容在 html5 中无法正确显示

html - 3 列到 2 列。多行

javascript - 在 safari 浏览器中使用 javascript 问题加载 html

html - 似乎无法使我的菜单居中

html - CSS 在 Firefox 中不工作,但在 Chrome 中工作

php - PDO MySQL fetchAll() 结果使用双倍的必要内存?

PHP - PDO 连接后区域设置信息消失 (DB2)

javascript - 拉伸(stretch) <img> 以适合整个屏幕,而不保持宽高比且没有滚动条

php - 如何在 symfony2 Controller 中迭代 ArrayCollection