导航首页 » 技术教程 » PHP:readfile()的用法_Filesystem函数
PHP:readfile()的用法_Filesystem函数 149 2023-12-15   

readfile

(PHP 4, PHP 5)

readfile — 输出一个文件

说明

int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )

读入一个文件并写入到输出缓冲。

参数

filename

要读取的文件名。

use_include_path

如果也想在 include_path 中搜索文件,可以使用可选的第二个参数并将其设为 TRUE

context

A context stream resource.

返回值

返回从文件中读入的字节数。如果出错返回 FALSE 并且除非是以 @readfile() 形式调用,否则会显示错误信息。

范例

Example #1 Forcing a download using 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('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

以上例程的输出类似于:

注释

Note:

readfile() will not present any memory issues, even when sending large files, on its own. If you encounter an out of memory error ensure that output buffering is off with ob_get_level().

Tip

如已启用fopen 包装器,在此函数中, URL 可作为文件名。关于如何指定文件名详见 fopen()。各种 wapper 的不同功能请参见 支持的协议和封装协议,注意其用法及其可提供的预定义变量。

Note: 在 PHP 5.0.0 中增加了对上下文(Context)的支持。有关上下文(Context)的说明参见 Streams。

参见

fpassthru() - 输出文件指针处的所有剩余数据 file() - 把整个文件读入一个数组中 fopen() - 打开文件或者 URL include - include require - require virtual() - 执行 Apache 子请求 file_get_contents() - 将整个文件读入一个字符串 支持的协议和封装协议


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103