2017-04-24 141 views
-1

我正在爲學校創建一個網站,我在使用php包含文件時遇到問題。PHP的包括和路徑

所以我以下結構:

  • 的index.php
  • 包括/
    • head.php
  • 風格/
    • 的style.css
  • 網站/
    • contact.php

現在我已經聯繫我的CSS文件中的header.php
而header.php文件包含在的index.php並在contact.php,

所述的header.php的代碼是:

<link href="style/style.css" rel="stylesheet" type="text/css"> 

的index.php文件的代碼是:

include 'include/head.php'; 

而且contact.php的代碼是:

include '../include/head.php'; 

但是服務器找不到css文件,當我打開contact.php

有人能幫助我嗎?

回答

0

您需要有關定義的根文件夾路徑相對文件位置

所以你需要做以下改變

對於根(絕對)路徑:

<link href="/style/style.css" rel="stylesheet" type="text/css"> 

或相對路徑:

<link href="../style/style.css" rel="stylesheet" type="text/css"> 
0

link元素改成這樣:

<link href="/style/style.css" rel="stylesheet" type="text/css"> 

起始斜線使其成爲絕對路徑,它指向您的域的根。

0

參考style/style.css意味着獲得這個路徑style/style.css從當前工作目錄中的/style/style.css指從document_root路徑,如果您使用此的任何文件,該文件將工作在所有情況下都完全正常。

更改爲:

<link href="style/style.css" rel="stylesheet" type="text/css"> 

此:

<link href="/style/style.css" rel="stylesheet" type="text/css">