search
Homephp教程php手册php fread函数与fread函数用法

php fread函数与fread函数用法

Jun 13, 2016 am 11:17 AM
freadphpresourcestringandfunctionTutorialusagegrammar

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.