如何包含来自不同目录的PHP脚本? [重复]

Possible Duplicate:
include file from different directory

I have a site laid out as follows:

  • index.php includes include/code.php
  • config.php
  • include/
    • code.php includes ./config.php
  • admin/
    • index.php includes code.php

When doing the inclusion from the primary index there are no problems, however when I try and include code.php from admin it fails to locate and include config.php

You have three options that I can think of:

  1. Specify the files absolute path in your includes, preferably using a constant containing your base directory. For example: require_once BASE_DIR . '/include/code.php';
  2. Specify relative paths from each file. To do this, you prefix the path with dirname(__FILE__) to ensure that the relative path is in fact relative to the current file (and not the executed file). For example: require_once dirname(__FILE__) . '/../include/code.php';
  3. Add your base directory to the include path, and specify the absolute path inside the project directory. For example: require_once '/include/code.php';

Try this:

//code inside admin/index.php
include('../config.php');