Use the PHP function fgets to read pointer files. PHP function fgets -- read a line from the file pointer. stringfgets (inthandle [, intlength]) reads a line from the file pointed to by handle and returns a line with a maximum length of-1 bytes.
PHP function fgets -- read a line of description from the file pointer
String fgets (int handle [, int length])
Read a row from the file pointed to by handle and return a string of up to length-1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value is 1 K, or 1024 bytes.
If an error occurs, FALSE is returned.
Common defects of PHP function fgets:
People who are used to the fgets () syntax in C should notice how EOF is returned.
The file pointer must be valid and point to a file successfully opened by fopen () or fsockopen.
Below is a simple example of a PHP function fgets: Example 1. read files row by row
- < ?php
- $handle = fopen("/tmp/
inputfile.txt", "r");
- while (!feof($handle)) {
- $buffer = fgets($fd, 4096);
- echo $buffer;
- }
- fclose($handle);
- ?>
Note: The length parameter is optional since PHP 4.2.0. If this parameter is ignored, the row length is assumed to be 1024. Starting from PHP 4.3, ignoring length will continue to read data from the stream until the row ends. If most of the rows in the file are larger than 8 kB, it is more effective to specify the length of the maximum row in the script to use resources.
Note: Starting from PHP 4.3, this function can be safely used for binary files. Earlier versions do not work.
Note: If the PHP function fgets does not recognize the row Terminator of the Macintosh File when reading the file, you can activate the auto_detect_line_endings runtime configuration option.
Delimiter -- read a line from the file pointer indicating string fgets (int handle [, int length]) read a row from the file pointed to by handle and return a maximum of length-1 bytes...