举报投诉联系我们 手机版 热门标签 名动网
您的位置:名动网 > php readfile PHP readfile() 函数

php readfile PHP readfile() 函数

2023-03-10 16:20 PHP教程

php readfile PHP readfile() 函数

php readfile

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);   // 读取并显示图片 

 

PHP readfile() 函数

PHP readfile() 函数


PHP Filesystem 参考手册 完整的 PHP Filesystem 参考手册

定义和用法

readfile() 函数读取一个文件,并写入到输出缓冲。

如果成功,该函数返回从文件中读入的字节数。如果失败,该函数返回 FALSE 并附带错误信息。您可以通过在函数名前面添加一个 '@' 来隐藏错误输出。

语法

readfile(filename,include_path,context)

参数 描述
filename 必需。规定要读取的文件。
include_path 可选。如果您还想在 include_path(在 php.ini 中)中搜索文件的话,请设置该参数为 '1'。
context 可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。


提示和注释

提示:如果在 php.ini 文件中 "fopen wrappers" 已经被激活,您可以在本函数中把 URL 作为文件名来使用。


实例

<?php
echo readfile("test.txt");
?>

上面的代码将输出:

There are two lines in this file.
This is the last line.
57


PHP Filesystem 参考手册 完整的 PHP Filesystem 参考手册
阅读全文
以上是名动网为你收集整理的php readfile PHP readfile() 函数全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 名动网 mdwl.vip 版权所有 联系我们