Home  >  Article  >  php教程  >  php fread函数与fread函数用法

php fread函数与fread函数用法

WBOY
WBOYOriginal
2016-06-13 11:17:471581browse

php fread函数与fread函数用法  

php教程 fread函数与fread函数用法
/*
fread语法:
string fread ( resource $handle , int $length )

fread()读取到的字节长度由处理引用的文件指针。读尽快停止对符合下列条件之一:

已经读取的字节长度
!eof(文件结束)达到
一包可用网络(流)
已阅读8192字节(打开后用户空间流)

*/

//fread读取文件实例一

$filename = "/www.bkjia.com/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

//php5以上版本读取远程服务器内容

$handle = fopen("http://www.bkjia.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);


//

$handle = fopen("http://down.php100.com/", "rb");
$contents = '';
while (!feof($handle)) {
  $contents .= fread($handle, 8192);
}
fclose($handle);

/*
有时流的目的不是用eof标记,也不是固定的标志,这就是为什么这个循环永远。这引起了我许多烦恼...
我解决它使用stream_get_meta_data功能,如下面显示一个break语句:

*/

$fp = fsockopen("mb.php100.com", 80);
if (!$fp) {
    echo "$errstr ($errno)
n";
} else {
    fwrite($fp, "data sent by socket");
    $content = "";
    while (!feof($fp)) { 
        $content .= fread($fp, 1024);
        $stream_meta_data = stream_get_meta_data($fp); //added line
         if($stream_meta_data['unread_bytes']     }
    fclose($fp);
    echo $content;
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn