如何在不同文件夹深度的php中包含相同的相对php?

如何在不同文件夹深度的php中包含相同的相对php?,php,zen-cart,Php,Zen Cart,如何在不同文件夹深度的php中包含相同的相对php? 出于某种原因,我需要在我的zencart网站中添加一个www/subam/index.php,这个php需要添加到www/includes/application_top.php,但是如果我需要(../includes/application_top.php),它将出错。怎么办? <?php /* index.php */ require('includes/application_top.php'); /* subam/index

如何在不同文件夹深度的php中包含相同的相对php?
出于某种原因,我需要在我的zencart网站中添加一个www/subam/index.php,这个php需要添加到www/includes/application_top.php,但是如果我需要(../includes/application_top.php),它将出错。怎么办?

<?php 
/* index.php */
require('includes/application_top.php');

/* subam/index.php */
// this will be wrong
require('../includes/application_top.php');
?>

始终使用物理完整路径,这样您就不会有任何问题:

php 5.3:

require(__DIR__ . '/lib/file.php');
如果您需要从您所在的位置返回,请使用../:

require(__DIR__ . '/../lib/file.php');
php 5.2及更低版本: 将
\uuuu DIR\uuuu
替换为
dirname(\uuuu文件)

在您的示例中:

/* index.php */
require(__DIR__ .  '/includes/application_top.php');

/* subam/index.php */
require(__DIR__ .  '/../includes/application_top.php');

您需要设置包含路径

set_include_path(get_include_path() . DIRECTORY_SEPARATOR . '..');

使用应用程序路径