Php 无法更改当前工作目录

Php 无法更改当前工作目录,php,directory,Php,Directory,我在根/lib中有header.php,它包括同一目录中的header_sub.php。通常根目录中的文件可以通过以下代码直接包含它们: include_once('lib/header.php'); 但是现在我在一个子目录/博客中有example.php,如果我使用这些 include_once(../'lib/header.php'); or include_once($_SERVER['DOCUMENT_ROOT'].'/lib/header.php'); or include_

我在根/lib中有header.php,它包括同一目录中的header_sub.php。通常根目录中的文件可以通过以下代码直接包含它们:

include_once('lib/header.php');
但是现在我在一个子目录/博客中有example.php,如果我使用这些

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将无法正确包含

有没有一种方法可以在不修改header.php和header_sub.php的情况下包含它们

一些机构建议使用这些工具:

 $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
然而,即使我可以看到当前url在chdir()之后是根目录,它仍然包含这个root/blog/lib……

使用include_once('/lib/header.php')

试着这样做:

$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

尝试一次include_('../lib/header.php');如果blog是根目录中的子目录。请注意,您没有正确传递路径:
include_once(../'lib/header.php')
应该是
include_once('../lib/header.php')