【问题标题】:how to include the same relative php in different folder depth php?如何在不同的文件夹深度 php 中包含相同的相对 php?
【发布时间】:2013-04-22 12:16:59
【问题描述】:

如何在不同文件夹深度的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');
?>

【问题讨论】:

  • 使用 application_path

标签: php zen-cart


【解决方案1】:

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

php 5.3:

require(__DIR__ . '/lib/file.php');

如果您需要从您所在的位置返回,请使用 ../:

require(__DIR__ . '/../lib/file.php');

php 5.2 及更低版本: 将__DIR__ 替换为dirname(__FILE__)

在你的例子中:

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

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

【讨论】:

    【解决方案2】:

    您需要设置包含路径

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

    【讨论】:

      猜你喜欢
      • 2021-07-05
      • 2012-08-06
      • 1970-01-01
      • 2013-10-25
      • 2013-04-09
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多