PHP readfile 函数用于读取文件并将其内容写入输出缓冲。它是一个非常有用的函数,可以用来显示文本文件、图像文件或者其他二进制数据。
readfile() 函数的语法如下:
int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )
参数 filename 是必需的,它指定要读取的文件名。如果 use_include_path 参数设置为 TRUE,则在 include_path 中搜索 filename。context 参数是可选的,它是一个资源类型,表示一个流上下文。
readfile() 函数会将文件内容读入到输出缓冲区中,然后立即输出。因此,在使用 readfile() 函数之前不能有任何输出操作。如果成功执行,readfile() 函数会返回读取的字节数。如果失败,则返回 FALSE。
下面的例子使用 readfile() 函数来显示一个 HTML 文件:
$filename = "test.html"; // 要显示的 HTML 文件名 readfile($filename); // 读取并显示 HTML 文件
上面代码中使用 readfile() 函数来显示 test.html 文件中的内容。
此外,readfile() 还可以用来显示图片、PDF 或者其他二进制数据文件。例如:
$filename = "image.jpg"; // 要显示的图片名 header("Content-type: image/jpeg"); // 让浏览器识别图片格式 readfile($filename); // 读取并显示图片
readfile() 函数读取一个文件,并写入到输出缓冲。
如果成功,该函数返回从文件中读入的字节数。如果失败,该函数返回 FALSE 并附带错误信息。您可以通过在函数名前面添加一个 '@' 来隐藏错误输出。
参数 | 描述 |
---|---|
filename | 必需。规定要读取的文件。 |
include_path | 可选。如果您还想在 include_path(在 php.ini 中)中搜索文件的话,请设置该参数为 '1'。 |
context | 可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。 |
提示:如果在 php.ini 文件中 "fopen wrappers" 已经被激活,您可以在本函数中把 URL 作为文件名来使用。
上面的代码将输出:
PHP image_type_to_extension - 获取图片后缀PHP 图像处理image_type_to_extension — 根据指定的图像类型返回对应的后缀名。语...
PHP imageaffinematrixget - 获取矩阵PHP 图像处理imageaffinematrixget — 获取矩阵。语法array imageaffinematrixget ( int $t...
PHP imagechar - 写出横向字符PHP 图像处理imagechar — 写出横向的字符。语法bool imagechar ( resource $image , int $fo...
PHP connection_aborted() 函数PHP Misc 参考手册实例 创建一个函数(check_abort()),在客户机终止脚本时写入一条日志消息:?p...
PHP die() 函数PHP Misc 参考手册实例 输出一条消息,并退出当前脚本:?php$site = //www..cn/; fopen($site,r) or die(Unable t...