In the PHP language, there are many powerful functions that support the language's continuous development, and the pie allows more and more programmers to start choosing the language. PHP function fgets is one of the powerful functions.
PHP function Fgets--Reads a line from the file pointer, the syntax is as follows
Fgets (handle, length)
Detailed parameters:
Parameters |
Description |
File |
Necessary. Specifies the file to be read. |
Length |
Optional. Specifies the number of bytes to read. The default is 1024 bytes. |
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/test.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.