2012-05-22 39 views
1

我使用這個代碼來定義我的項目的目錄路徑:在PHP中使用未定義的常量,而generationg目錄路徑

的config.php是

<?php 
define('CB_HOME', realpath(dirname(__DIR__))); 
define('FD_HOME', CB_HOME."/testbot"); 

我用我在使用CB_home而FD_home其他文件。

我得到的錯誤:

PHP Notice: Use of undefined constant CB_HOME - assumed 'CB_HOME' in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3 
PHP Warning: require_once(CB_HOME/AbstractCbRest.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3 
PHP Fatal error: require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'CB_HOME/AbstractCbRest.php' (include_path='.;C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3 

我的bootstrap.php是:

<?php 
require_once 'config.php'; 
require_once CB_HOME.'/AbstractCbRest.php'; 

它在哪兒去了?

+7

和恆在'config.php'界定?你確定你正在查看* correct *'config.php'嗎? – deceze

+0

嘗試在定義它之前連接FD家庭值。 – IEnumerable

+0

我試過同樣的錯誤仍然存​​在相同的錯誤 – Shiven

回答

1

我的猜測是,include_path設置與干擾你的腳本。所以(正如@deceze所提到的)你可能正在加載一個不正確的(儘管現有的)config.php文件。

也許你可以嘗試使用絕對路徑,包括您的配置,或者使用的線沿線的東西:

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.php'; 
+0

非常感謝。的確include_path設置干擾了腳本。已經給絕對路徑了。 – Shiven

-1

很明顯,具有兩個「\」的窗口路徑不起作用。要刪除一個雙反斜線(注:這是沒有辦法解決,但解決方法),這樣做:

$someValue = 'C:\\xampp\\htdocs\\test-bot\\testbot\\'; $someValue = preg_replace('/\\\\/u', '\\', $someValue); 

或在您的情況:

define('CB_HOME', preg_replace('/\\\\/u', '\\', realpath(dirname(DIR)))); 
+2

爲什麼使用'DIR'而不是'__DIR__'應該有所幫助? '__DIR__'是一個*魔術常數*在PHP中... – Oleg

+0

嘗試過但沒有幫助 – Shiven

+0

男人,我不知道,但有一個帖子之前建議...被刪除,但。 – systrue