Home > Article > Backend Development > php fgets function_PHP tutorial
php fgets function
fgets
(PHP 4, PHP 5)
fgets - Get line pointer from file
Description
String fgets(resource$handle[summary$length])
Get line pointer from file.
Parameters
Handle
The file pointer must be valid and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).
Length
Read when end length - 1 byte is read, newline (which is included in the return value), or EOF parsing (whichever comes first). If no length is specified, reading of the stream continues until the end of the line is reached.
Note: Prior to PHP 4.3.0, omissions would assume a line length of 1024. If most of the lines in the file are larger than 8KB, it is more efficient for your script to specify a maximum line length.
Return value
Returns a string of length up to - 1 byte read from the file pointed out by processing.
If an error occurs, returns FALSE.
Modify
Release Notes
4.3.0 fgets() is now binary safe
4.2.0 The length parameter is optional
Look at an example:
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
While (!feof($handle)) {
$buffer = fgets($handle, 4096);
echo $buffer;
}
fclose($handle);
}
?>