fread() - 读取文件(可安全用于二进制文件) - php 文件目录函数
fread()
(PHP 4, PHP 5, PHP 7)
读取文件(可安全用于二进制文件)
说明
fread(resource $handle,int $length): stringfread()从文件指针$handle读取最多$length个字节。该函数在遇上以下几种情况时停止读取文件:
- 读取了$length个字节
- 到达了文件末尾(EOF)
- a packet becomes available or thesocket timeoutoccurs (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.
参数
$handle文件系统指针,是典型地由fopen()创建的resource(资源)。
$length最多读取$length个字节。
返回值
返回所读取的字符串,或者在失败时返回FALSE
。
范例
一个简单的fread()例子
Binaryfread()example
Warning在区分二进制文件和文本文件的系统上(如 Windows)打开文件时,fopen()函数的 mode 参数要加上'b'。
Remotefread()examples
Warning当从任何不是普通本地文件读取时,例如在读取从远程文件或popen()以及fsockopen()返回的流时,读取会在一个包可用之后停止。这意味着应该如下例所示将数据收集起来合并成大块。
注释
Note:如果只是想将一个文件的内容读入到一个字符串中,用file_get_contents(),它的性能比上面的代码好得多。Note:
Note thatfread()reads from the current position of the file pointer.Useftell()to find the current position of the pointer andrewind()to rewind the pointer position.
参见
fwrite()
写入文件(可安全用于二进制文件)fopen()
打开文件或者 URLfsockopen()
打开一个网络连接或者一个Unix套接字连接popen()
打开进程文件指针fgets()
从文件指针中读取一行fgetss()
从文件指针中读取一行并过滤掉 HTML 标记fscanf()
从文件中格式化输入file()
把整个文件读入一个数组中fpassthru()
输出文件指针处的所有剩余数据ftell()
返回文件指针读/写的位置rewind()
倒回文件指针的位置
I couldn't get some of the previous resume scripts to work with Free Download Manager or Firefox. I did some clean up and modified the code a little. Changes: 1. Added a Flag to specify if you want download to be resumable or not 2. Some error checking and data cleanup for invalid/multiple ranges based on http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt 3. Always calculate a $seek_end even though the range specification says it could be empty... eg: bytes 500-/1234 4. Removed some Cache headers that didn't seem to be needed. (add back if you have problems) 5. Only send partial content header if downloading a piece of the file (IE workaround) The problem is that sometimes end of streaming is not marked by EOF nor a fixed mark, that's why this looped forever. This caused me a lot of headaches... I solved it using the stream_get_meta_data function and a break statement as the following shows: Hope this will save a lot of headaches to someone. (Greetings, from La Paz-Bolivia)
This is an hack I've done to download remote files with HTTP resume support. This is useful if you want to write a download script that fetches files remotely and then sends them to the user, adding support to download managers (I tested it on wget). To do that you should also use a "remote_filesize" function that you can easily write/find.
Note that fread() will return '' (empty string) when a timeout occurs unlike socket_read() which returns false...
I thought I had an issue where fread() would fail on files > 30M in size. I tried a file_get_contents() method with the same results. The issue was not reading the file, but echoing its data back to the browser. Basically, you need to split up the filedata into manageable chunks before firing it off to the browser: Hope this helps someone!
Changing the value of $length may yield to different download speeds when serving a file from a script. I was not able to max out my 10mbps connection when 4096 was used. I found out that using 16384 would use all the available bandwidth. When outputing binary data with fread, do not assume that 4096 or 8192 is the optimal value for you. Do some benchmarks by downloading files through your script.
My script was based on example 3b, but used up 100% CPU when a timeout occurred that wasn't "seen". This is very bad. So here's my code, hoping this will help people out there with the same problem. Obviously first use $rPage = fsockopen(...) and fwrite($rPage,...) and such, after which: $sPage = ''; // the page goes in here $iTimeout = 5; // set the timeout in seconds stream_set_timeout($rPage,$iTimeout); stream_set_blocking($rPage,0); $fTimeout = microtime(true); do { if (($sRead = fread($rPage,8192))!==false and strlen($sRead)) { $sPage .= $sRead; } else { usleep(10000); } // 0.01 second $aInfo = stream_get_meta_data($rPage); } while (!feof($rPage) and !$aInfo['timed_out'] and microtime(true)-$fTimeout
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!