无法更改当前工作目录

时间:2013-06-24 06:48:28

标签: php directory

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

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将无法正确包含。<​​/ p>

有没有办法在不修改它们的情况下包含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 ......

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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