2013-08-27 146 views
0

我有一個位於/fileupload/index.php的腳本。我們的名字A. ,我想包括腳本里面A.讓我們將它B. B位於名/includes/class/class.product.phpPHP腳本絕對路徑

我怎麼能包括B內部的真實路徑使用(目錄名(FILE))?

如果A位於的index.php我包括樣B一樣......

require_once realpath(dirname(__FILE__)).'/includes/class/class.product.php'; 

但主要的問題是一個位於/fileupload/index.php。我該怎麼辦 ?我不想硬編碼它。這就是爲什麼我問。

+0

相對路徑呢? – hendr1x

回答

0

首先,你必須定義SITE_ROOT,這樣

// PAGE_FILE = this is the path of the current file 
// SITE_ROOT = this is the root of your site 
if (DIRECTORY_SEPARATOR == '/') { 
    //the site is hosted in linux server 
    define('PAGE_FILE', __FILE__); 
} else { 
    //the site is hosted in Windows server, so some extra processing is necessary 
    define('PAGE_FILE', str_replace(DIRECTORY_SEPARATOR, '/', __FILE__)); 
} 
define('SITE_ROOT', substr(PAGE_FILE, 0, -(mb_strlen($_SERVER['PHP_SELF'])))); 

現在,只需波紋管編寫代碼,包括你的文件,無論它位於何處:

include SITE_ROOT . '/includes/class/class.product.php' 
include SITE_ROOT . '/my-path/another-file.php' 
include SITE_ROOT . '/file-in-root-directory.php' 

希望這會給你一個永久的解決方案。