PHP function Fgets--reading a line from the file pointer
string fgets (int handle [, int length])
Reads a row from the file pointed to by handle and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (see first the case). If length is not specified, the default is 1 K, or 1024 bytes.
Returns FALSE when an error occurs.
PHP functions fgets Common flaws:
People who are accustomed to the fgets () syntax in C should be aware of how EOF is returned.
The file pointer must be valid and must point to a file that was successfully opened by fopen () or Fsockopen ().
Here is a simple example of a PHP function Fgets: Example 1. Read a file line-wise
- < ? PHP
- $ Handle fopen("/tmp/
Inputfile.txt "," R ");
- while (!feof ($handle)) {
- $ Buffer fgets($FD, 4096);
- Echo $buffer;
- }
- Fclose ($handle);
- ?>
Note: The length parameter becomes optional from PHP 4.2.0, and if omitted, the line is assumed to be 1024. starting with PHP 4.3, ignoring length will continue to read data from the stream until the end of the line. If most of the rows in the file are larger than 8KB, specifying the length of the largest row in the script is more efficient with resources.
Note: starting with PHP 4.3 This function can be used safely in binary files. The earlier versions were not.
Note: You can activate the Auto_detect_line_endings runtime configuration option if you encounter a PHP function that does not recognize the line terminator of a Macintosh file when reading a file fgets.
http://www.bkjia.com/PHPjc/445958.html www.bkjia.com true http://www.bkjia.com/PHPjc/445958.html techarticle PHP function Fgets--reads a line from a file pointer to a string fgets (int handle [, int length]) reads a row from a file pointed to by handle and returns a maximum length of length-1 bytes ...