0

I have header.php in the root/lib which is including header_sub.php in the same directory. Normally files in root can directly include them by this code:

include_once('lib/header.php');

but now i have example.php in a sub-directory /blog, if i use these

include_once(../'lib/header.php');  or 
include_once($_SERVER['DOCUMENT_ROOT'].'/lib/header.php');  or 
include_once(dirname(__FILE__).'/lib/header.php');

header_sub.php would not be included correctly.

Is there a way to include header.php and header_sub.php without modifying them?

Some body suggested to use these:

 $oldcwd = getcwd(); // Save the old working directory
    chdir("../"); // Moves up a folder, so from /blog to /
    include("header.php"); // Include the file with the working directory as if the header file were being loaded directly, from it's folder
    chdir($oldcwd); // Set the working directory back to before

However, even i can see the current url is root directory after chdir(), it still includes this root/blog/lib......

4

2 に答える 2

0

Use include_once('/lib/header.php');

于 2013-06-24T06:52:04.940 に答える
0

このようにしてみてください:

$oldcwd = getcwd();  // Save the old working directory
chdir($_SERVER['DOCUMENT_ROOT']);
include_once('lib/header.php');
chdir($oldcwd);      // Go back to the old working directory
于 2013-09-22T21:26:30.533 に答える