Phpfread function and fread function usage. Phpfread function and fread function usage php Tutorial fread function and fread function usage * fread syntax: stringfread (resource $ handle, int $ length) fread () the length of the byte read is used by the processing of the php fread function and the fread function.
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 determined by the pointer to the file being referenced. Read as soon as possible to meet one of the following conditions:
Length of read bytes
! Eof (end of file) reaches
One package available network (stream)
8192 bytes read (User space stream opened)
*/
// Fread reads file instance 1
$ Filename = "/www.bkjia.com/local/something.txt ";
$ Handle = fopen ($ filename, "r ");
$ Contents = fread ($ handle, filesize ($ filename ));
Fclose ($ handle );
// Php5 and later versions read remote server content
$ 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 a stream is not to use the eof mark, nor is it a fixed sign, which is why this loop is always used. This caused me a lot of troubles...
I solve this problem by using the stream_get_meta_data function. the following shows 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.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445443.htmlTechArticlephp fread function and fread function usage php Tutorial fread function and fread function usage/* fread syntax: string fread (resource $ handle, int $ length) fread () the length of the read bytes is introduced by processing...