PHP fread function and fread function usage _php Tutorial

Source: Internet
Author: User
PHP fread function and fread function usage

PHP tutorial fread function and Fread function usage
/*
Fread Syntax:
String Fread (Resource $handle, int $length)

Fread () reads the length of the bytes to the file pointer that is referenced by the process. Read stop as soon as possible to meet one of the following conditions:

Length of bytes already read
!eof (end of document) reached
A packet of available networks (streams)
Read 8192 bytes (open after user space flow)

*/

Fread Read File instance one

$filename = "/www.bkjia.com/local/something.txt";
$handle = fopen ($filename, "R");
$contents = Fread ($handle, FileSize ($filename));
Fclose ($handle);

Read remote server content PHP5 or later

$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);

/*
Sometimes the purpose of the stream is not to be marked with EOF, nor is it a fixed flag, which is why this loop is forever. This caused me a lot of trouble ...
I resolved it using the Stream_get_meta_data feature, as shown below is a break statement:

*/

$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 '] <= 0) break; Added line
}
Fclose ($FP);
Echo $content;
}

http://www.bkjia.com/PHPjc/445443.html www.bkjia.com true http://www.bkjia.com/PHPjc/445443.html techarticle php fread function and fread function usage PHP tutorial fread function and Fread function usage/* fread syntax: string fread (Resource $handle, int $length) fread () Read the word Section length by processing ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.