为什么“include_once”成功地为html服务,而“readfile”在php中失败


Why does `include_once` successfully serves the html while `readfile` fails in php?

我有以下函数

public function my_index()
{
    try {
        define("PATH_ROOT", dirname(__FILE__));
        var_dump(PATH_ROOT);
        include_once PATH_ROOT . "/test.php";
        //readfile('./test.php');
    } catch (Exception $e) {
        var_dump($e->getMessage());
    }
    die;
}

在我的test.php中,我有

<div>This is test</div>

我验证test.php与承载my_index 的文件在同一目录中

但每当我尝试时

readfile('./test.php);

它总是返回

readfile(./test.php): failed to open stream: No such file or directory

当我评论readfile时,它实际上能够服务于test.php 的内容

有人能解释一下include_once为什么提供html吗?这是在php中提供html的惯用方法吗?

您为include_one提供了绝对路径。

 include_once PATH_ROOT . "/test.php";

对另一个尝试相同的。

readfile(PATH_ROOT . "/test.php");

注:您可以使用getcwd()检查当前工作目录