(PHP 4, PHP 5, PHP 7)
fread — 锟斤拷取锟侥硷拷锟斤拷锟缴帮拷全锟斤拷锟节讹拷锟斤拷锟斤拷锟侥硷拷锟斤拷
$handle
, int $length
) : string
fread() 锟斤拷锟侥硷拷指锟斤拷
handle
锟斤拷取锟斤拷锟�
length
锟斤拷锟街节★拷
锟矫猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟铰硷拷锟斤拷锟斤拷锟绞蓖V癸拷锟饺★拷募锟斤拷锟�
length
锟斤拷锟街斤拷
锟斤拷锟斤拷锟斤拷锟斤拷取锟斤拷锟街凤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷失锟斤拷时锟斤拷锟斤拷 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
锟斤拷锟斤拷锟街讹拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟侥憋拷锟侥硷拷锟斤拷系统锟较o拷锟斤拷 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
锟斤拷锟斤拷锟轿何诧拷锟斤拷锟斤拷通锟斤拷锟斤拷锟侥硷拷锟斤拷取时锟斤拷锟斤拷锟斤拷锟节讹拷取锟斤拷远锟斤拷锟侥硷拷锟斤拷 popen() 锟皆硷拷 fsockopen() 锟斤拷锟截碉拷锟斤拷时锟斤拷锟斤拷取锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷之锟斤拷停止锟斤拷锟斤拷锟斤拷味锟斤拷应锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷示锟斤拷锟斤拷锟斤拷锟秸硷拷锟斤拷锟斤拷锟较诧拷锟缴达拷椤�
<?php
// 锟斤拷 PHP 5 锟斤拷锟斤拷锟竭版本
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
<?php
$handle = fopen("http://www.example.com/", "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.