2016-07-02 64 views
-2

我在目錄My中有index.php文件。如何在PHP中包含文件?

此文件已包含文件的core/coreEngine.php

裏面coreEngine.php我嘗試包括另一個文件config/test.php位於上一層目錄:

我嘗試了所有的情況:

require_once '../config/test.php'; 
require_once '../../config/test.php'; 
require_once '.../config/test.php'; 
+0

[PHP是否包含相對於文件或調用代碼的路徑?](http://stackoverflow.com/questions/7378814/are-php-include-paths-相對於文件或者調用代碼) – FirstOne

+0

你的目錄結構對我來說還不清楚,你能否請[提供圖表](http://stackoverflow.com/a/3455675/4233593) –

回答

2
My/index.php 
My/core/coreEngine.php <--- you are here 
My/config/test.php  <--- you want to go here 

你想回到M y目錄,所以2個目錄返回:

require_once '../../config/test.php'; 

但我強烈建議使用絕對路徑,而不是相對的路徑。您可能想要使用類似於:

require_once($_SERVER['DOCUMENT_ROOT'].'/config/test.php');