首先,你必须定义 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'
我希望这会给你一个永久的解决方案。