2012-02-22 42 views
0

我知道我在做一些愚蠢的事,可能很明顯,但我不知道是什麼。在我header.php我:

define('ABSPATH', $_SERVER['SERVER_NAME']); 
require_once(ABSPATH . "/config.php"); 

使用此產生以下錯誤:

Warning: require_once(localhost/config.php): failed to open stream: 
No such file or directory in C:\wamp\www\inc\header.php on line 4 
Call Stack: 0.0002 684176 1. {main}() C:\wamp\www\index.php:0 0.0003 
696152 2. require_once('C:\wamp\www\inc\header.php') C:\wamp\www\index.php:2 
Fatal error: require_once(): Failed opening required 'localhost/config.php' (include_path='.;C:\php\pear') in C:\wamp\www\inc\header.php on line 4 Call Stack: 
0.0002 684176 1. {main}() C:\wamp\www\index.php:0 0.0003 696152 2. require_once('C:\wamp\www\inc\header.php') C:\wamp\www\index.php:2 

但是,如果我直接導​​航到localhost/config.php(目前,它只是在附和了一句簡單的1行文件)它會工作。

我在做什麼錯?

回答

3

$_SERVER['DOCUMENT_ROOT']應該做的伎倆

+2

+1你是正確的,爲什麼'本地主機/ config.php'在他的瀏覽器的工作原理的解釋,但不要求將是很好的,但。描述'URL的fopen wrappers'以及如何與他們打開'要求(「HTTP://」 ABSPATH「/config.php」。)'會的工作,爲什麼這是一個壞主意,都將是不錯的。 – Paulpro 2012-02-22 00:25:56

+0

這樣做的竅門,謝謝! @ PaulP.R.O。 - 你能解釋爲什麼這是一個壞主意嗎?我應該使用什麼? – 2012-02-22 00:30:57

+1

在'http'中使用'require'是個壞主意,因爲你所有的require/includes都將通過http來完成。這是沒有必要的,因爲你的文件在同一臺服務器上,你可以打開它。 '$ _SERVER ['DOCUMENT_ROOT']'會給你的webroot文件系統路徑。從那裏你可以訪問config.php。 – Paulpro 2012-02-22 00:35:20

0

你確定你不是故意的用一個反斜槓您的Windows系統上,在那裏?

此外,還要確保該文件是在localhost文件夾Web根內。

2

include S和其他任何從與文件系統路徑服務器工作見過文件的工作。 C:\foo\bar是一個文件系統路徑。 localhost不是,localhost是從網絡中看到的URL的主機名。當通過網絡處理爲URL時,文件localhost/config.php有效。在本地系統上的同一個文件有像C:\wamp\www\config.php的路徑,這是你需要使用require_once它是什麼。

0

嘗試以下:

define('ABSPATH', $_SERVER['DOCUMENT_ROOT']); 
require_once(ABSPATH . "/config.php");