0

我有一个位于 /fileupload/index.php 的脚本。让我们命名 A。我想在 A 中包含一个脚本。让我们将其命名为 B。B 位于 /includes/class/class.product.php

如何使用 realpath(dirname( FILE )) 在 A 中包含 B?

如果 A 位于 /index.php 我会像 B 一样包含...

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

但主要问题是A位于/fileupload/index.php。我应该怎么办 ?我不想硬编码它。这就是我问的原因。

4

1 回答 1

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'

我希望这会给你一个永久的解决方案。

于 2014-01-24T16:57:07.863 回答