readfile
(PHP 4, PHP 5, PHP 7)
readfile - 输出文件
描述
int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )
读取文件并将其写入输出缓冲区。
参数
filename
正在读取的文件名。
use_include_path
如果要在include_path中搜索文件,则可以使用可选的第二个参数并将其设置为TRUE
。
context
上下文流资源。
返回值
返回从文件中读取的字节数。如果发生错误,则返回FALSE
,除非该函数被调用为@ readfile(),则会打印一条错误消息。
示例
示例#1使用readfile()强制下载
<?php
$file = 'monkey.gif';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
?>
上面的例子会输出类似于:

笔记
注意: 即使在发送大文件时,readfile()也不会出现任何内存问题。如果遇到内存不足错误,请确保使用ob_get_level()关闭输出缓冲。
提示
注意:使用PHP 5.0.0添加了上下文支持。有关上下文的描述,请参阅流。
另请参阅
- fpassthru() - 输出文件指针上的所有剩余数据
- file() - 将整个文件读入一个数组
- fopen() - 打开文件或URL
- include - 包括
- require - 要求
- virtual() - 执行Apache子请求
- file_get_contents() - 将整个文件读入一个字符串
← popen
readlink →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com