如何使用PHP从不同目录中获取文件

时间:2015-04-22 17:47:05

标签: php

我理解为HTML,您可以使用../../../来包含其他文件夹中包含的内容。

我有这个PHP代码,我想知道如何在header.php之前使用../../../?

<?php
ob_start();
include("../../../header.php");
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","Homepage",$buffer);
echo $buffer;
?>

1 个答案:

答案 0 :(得分:0)

你可以使用chdir() php函数和getcwd()。我建议你使用常量作为源路径位置..

试试这个:

<?php
ob_start();
chdir("../../../");
$cwd =  rtrim(str_replace("\\", "/", getcwd()), '/').'/';
include($cwd."header.php");
$buffer=ob_get_contents();
ob_end_clean();
$buffer=str_replace("%TITLE%","Homepage",$buffer);
echo $buffer;
?>