2014-03-18 185 views
0

在我的public_html文件夾中,我有以下目錄:PHP包括和相對文件路徑

- includes (in here I have header.php which is included in index.php) 
- images 
- styles 
- scripts 

index.php包括header.php,這是我引用我的樣式,腳本,圖片等

一切都很好直到我嘗試直接從包含header.php的includes目錄中加載頁面,例如:

正在工作:www.mysite.com/index.php

不工作:www.mysite.com/includes/example_page.php(其中例如頁面還包括的header.php)

example.ph p沒有樣式表,圖片或腳本的加載。這是因爲它正在public_html/includes中查找它們。很顯然,將文件路徑更改爲../styles/stylesheet.css的作品。但這不是一個理想的解決方案,特別是如果我必須更改header.php中每個圖像的文件路徑。

如何檢測包含子目錄中的頁面是否將文件路徑設置爲指向public_html?

chdir();似乎沒有工作。

回答

0

在HTML/CSS中使用絕對路徑,例如

<link rel="stylesheet" href="/css/styles.css" /> 
          ^--- start at top of site document root 

<img src="/images/kittens.jpg" /> 
      ^--- start at top of site document root 

然後,你把你的腳本就不會磨砂的的HREF/SRC公司將始終啓動你的網站的網址空間的頂部和工作的方式了。

您將失去的東西是在子目錄中託管此特定網站的能力,例如,如果你決定將它移動到

http://example.com/oldsite/index.php 

所有的路徑會在href/src目錄將不得不重新編寫說/oldsite/images/kittens.jpg/oldsite/css/styles.css