php fread function and fread function usage

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags content file function network network programming php php tutorial programming

php tutorial fread function and fread function usage
/ *
fread syntax:
string fread (resource $ handle, int $ length)

The length of the byte read by fread () is handled by the file pointer referenced. Read as soon as possible to one of the following conditions:

The byte length that has been read
! eof (end of file) reaches a package of available networks (streams)
8192 bytes have been read (user space flow opened)

* /

// fread read the file instance one

$ filename = "/www.jzread.com/local/something.txt";
$ handle = fopen ($ filename, "r");
$ contents = fread ($ handle, filesize ($ filename));
fclose ($ handle); // php5 or later reads the contents of the remote server

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

$ handle = fopen ("http://down.jzread.com/", "rb");
$ contents = '';
while (! feof ($ handle)) {
$ contents. = fread ($ handle, 8192);
}
fclose ($ handle); / *
Sometimes the purpose of streaming is not marked with eof, nor is it a fixed sign, which is why this loop forever. This caused me a lot of trouble ...
I solved it using the stream_get_meta_data function as shown below with a break statement:

* /

$ fp = fsockopen ("mb.jzread.com", 80);
if (! $ fp) {
echo "$ errstr ($ errno) <br /> 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;
}

Related Article

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.