2011-06-25 150 views
1

我已經設置的Apache,使所有的請求到一個文件/var/www/common_index.phpPHP包括路徑問題

現在,common_index.php着眼於請求的文件名和源相應的文件 在/ var/WWW /123/public_html/requested_file.php

我在包含requested_file.php文件(使用相對路徑)時遇到了問題。它試圖搜索/ var/www中的文件而不是/ var/www/123/public_html/

如何解決此問題?

回答

6

你可以在requested_file.phpchange the working directory呼籲include使之前的文件的路徑它的工作原理:

chdir(dirname(__FILE__)); 
include 'path/to/file.php'; 

或PHP 5.3+

chdir(__DIR__); 
include 'path/to/file.php'; 

如果你不想改變工作目錄(這將影響到其他文件系統操作),然後只需每次追加的路徑,你做一個包括使用magic constant__DIR__

include dirname(__FILE__) . '/path/to/file.php'; 
include __DIR__ . '/path/to/file.php'; # for PHP 5.3+ 

其中路徑是相對於您使用上述代碼的文件。

+0

非常感謝!這看起來正是我所需要的。 – tanon

0

在Apache配置中編輯DocumentRoot。

將其設置爲/var/www/123/

+0

那麼我如何確保所有請求都轉到/var/www/common_index.php? (我出於某種原因需要它)。另外... public_html路徑各不相同.... – tanon

+0

檢查來自Arvin的答案。 – powtac

0

您可以使用__DIR__

$path = __DIR__; 

$path包含有這樣一行