2015-10-26 47 views
0

我正在使用PHP爲所有頁面提供相同的頁眉,導航和頁腳元素,但我遇到了問題;由於某些原因,PHP文件是不承認一個包括了header.html文件( 「/ header.html中」)PHP無法找到我的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> 

然後我的頭。 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頁面

+0

只是順便說一句,你可能不應該有一個html,在該header.html中文件頭標籤,因爲你已經有那些你的php文件 – Terminus

回答

3

當您使用<?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'); 
+0

所以我會更好地說<?php include(「/ public_html/header.html」); ?>如果是這樣,實際上是從我的文件系統根目錄的正確路徑? 我試圖讓一個header.html文件適用於多個目錄的整個站點。 – SFN

+0

'__DIR__'是一個常量,它將返回當前的相對目錄,所以如果你在php文件'/ path/to/public_html/index.php'中並使用'__DIR__',它將返回'/ path/to/public_html ' – skrilled

+0

好吧,不過我在/path/to/public_html/game-types/webpage.php中,但是我也在/ path/to/public_html /和其他想要訪問這個頭文件的目錄中有文件,不僅在/ path/to/public_html /目錄中。 – SFN

相關問題