Php/docs/function.fread
fread
(PHP 4, PHP 5, PHP 7)
fread — 读取文件(可安全用于二进制文件)
说明
fread
( resource $handle
, int $length
) : string
fread() 从文件指针
handle
读取最多
length
个字节。
该函数在遇上以下几种情况时停止读取文件:
- 读取了
length
个字节 - 到达了文件末尾(EOF)
- a packet becomes available or the socket timeout occurs (for network streams)
- if the stream is read buffered and it does not represent a plain file, at most one read of up to a number of bytes equal to the chunk size (usually 8192) is made; depending on the previously buffered data, the size of the returned data may be larger than the chunk size.
返回值
返回所读取的字符串, 或者在失败时返回 false
。
范例
Example #1 一个简单的 fread() 例子
<?php// get contents of a file into a string$filename = "/usr/local/something.txt";$handle = fopen($filename, "r");$contents = fread($handle, filesize($filename));fclose($handle);?>
Example #2 Binary fread() example
Warning 在区分二进制文件和文本文件的系统上(如 Windows)打开文件时,fopen() 函数的 mode 参数要加上 'b'。
<?php$filename = "c:\\files\\somepic.gif";$handle = fopen($filename, "rb");$contents = fread($handle, filesize($filename));fclose($handle);?>
Example #3 Remote fread() examples
Warning 当从任何不是普通本地文件读取时,例如在读取从远程文件或 popen() 以及 fsockopen() 返回的流时,读取会在一个包可用之后停止。这意味着应该如下例所示将数据收集起来合并成大块。
<?php// 对 PHP 5 及更高版本$handle = fopen("http://www.example.com/%22, "rb");$contents = stream_get_contents($handle);fclose($handle);?>
<?php$handle = fopen("http://www.example.com/%22, "rb");$contents = ;while (!feof($handle)) { $contents .= fread($handle, 8192);}fclose($handle);?>
注释
Note:
如果只是想将一个文件的内容读入到一个字符串中,用 file_get_contents(),它的性能比上面的代码好得多。
Note:
Note that fread() reads from the current position of the file pointer. Use ftell() to find the current position of the pointer and rewind() to rewind the pointer position.