2015-04-03 83 views
0

我有一個使用require_once的問題。 我指定了錯誤的路徑,找不到解決方案。使用require_once的錯誤路徑

我有一個名爲header.php的文件,它使用require_once:functions.php和navigation.php包含兩個文件。 這部分工作正常。 當我嘗試將header.php包含在位於不同目錄中的名爲view.php的文件中時,會出現此問題。

這裏是樹狀結構:

C:\wamp\www\1.1\plugins\docreader\php\view.php 
C:\wamp\www\1.1\theme\dark-blue\templates\files\header.inc.php 
C:\wamp\www\1.1\theme\dark-blue\templates\files\functions.inc.php 
C:\wamp\www\1.1\theme\dark-blue\templates\files\navigation.inc.php 

我嘗試了很多不同的路徑,但沒有成功。

請問有人有線索嗎?

+1

你只需要一點['MAGIC'](http://php.net/manual/en/language.constants.predefined.php) – Rizier123 2015-04-03 17:41:14

+2

很高興我的php日子已經結束xD – iamwhitebox 2015-04-03 17:42:24

回答

0

你需要讓你的文件路徑是絕對的而不是相對的。一個有用的超級全球實現這個是$_SERVER['DOCUMENT_ROOT']這將評估您的Web服務器文件根目錄。

+0

謝謝,我會嘗試。 – 2015-04-03 18:13:49

1

閱讀(和使用)魔術常數__DIR__和函數dirname()以從包含器的路徑開始生成包含文件的路徑。

例如,如果在plugins\docreader\php\view.php要包括theme\dark-blue\templates\files\functions.inc.php然後使用這樣的事情:

// Use this in 'plugins\docreader\php\view.php' 
include dirname(dirname(dirname(__DIR__))). 
     '/theme/dark-blue/templates/files/functions.inc.php'; 

__DIR__magic constant計算結果爲包含在那裏使用的文件的目錄。在C:\wamp\www\1.1\plugins\docreader\php\view.php中,__DIR__的值是'C:\wamp\www\1.1\plugins\docreader\php'

函數dirname()返回提供的路徑的父目錄。那種..,只有更好。三次使用它將作爲參數傳遞的值(上面解釋的值__DIR__)減少到'C:\wamp\www\1.1'。一切都從這裏直線前進:將相對路徑添加到所需的文件('/theme/dark-blue/templates/files/functions.inc.php'),並忘記包含問題。

+0

感謝您提供答案的北京。 – 2015-04-03 17:57:05

+0

謝謝你的提示回答。我嘗試了你的建議,但它不起作用。我認爲問題來自於header.php包含兩個其他文件。 header.php是我試圖包含在view.php中的一個。我試圖在view.php中包含其他兩個,但問題仍然存在。 – 2015-04-03 18:13:03

+0

注意:使用未定義常量SITE_TEMPLATES_PATH - 在第3行C:\ wamp \ www \ 1.1 \ theme \ dark-blue \ templates \ files \ header.inc.php中假定爲'SITE_TEMPLATES_PATH' 警告:require_once(SITE_TEMPLATES_PATH/files /第3行中C:\ wamp \ www \ 1.1 \ theme \ dark-blue \ templates \ files \ header.inc.php中沒有這樣的文件或目錄 致命錯誤:require_once ():在C:\ wamp \ www \ 1.1 \ theme \ dark-blue \ templates \ files中打開所需的'SITE_TEMPLATES_PATH/files/functions.inc.php'(include_path ='.; C:\ php \ pear')失敗\ 3號線上的\ header.inc.php – 2015-04-03 18:49:41