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;
}