php readfile() 术语

php readfile() terminology

当我们说一个函数的结果被发送到输出缓冲区时,这意味着结果在回显之前是不可见的。对于直接输出的函数,我们使用 ob_start 将结果定向到输出缓冲区(在它作为普通 html 到达浏览器之前)以便我们可以对其进行操作,然后如果需要,回显它. 函数 readfile() 的结果是直接可见的,例如某个文本文件的内容。那么我的问题是:

为什么 php 文档中提到 readfile() 将内容发送到输出缓冲区?! (而实际上是直接显示)。

我是不是漏掉了什么?

When we say that a function's result is sent to output buffer, it means that the result is not visible until echoed. 不,不是这个意思!

因为它被发送到输出缓冲区,它(除非你在将输出发送到浏览器之前明确执行 ob_start() 来缓冲输出)被直接发送到浏览器(或其他任何东西) ). 所有输出都发送到输出缓冲区,但除非您在发送之前将缓冲区设置为保存输出,否则它会被发送。

编辑

当然,可以将网络服务器配置为在发送一定量的数据之前缓存输出,但这与 PHP 输出缓冲

不同

嗯,当readfile()执行时,输出直接到stdout

当 php 用作 bash 脚本时了解此行为很有用:

#!/usr/bin/php

<?php

readfile("cool.txt"); 

// Output cool.txt whole content

从这个角度来看,它的行为与 file_get_contents 不同。

#!/usr/bin/php

<?php

file_get_contents("js.txt");

// No output!

$cool = file_get_contents("js.txt");

echo $cool;

// Output cool.txt