PHP fgets()Functions
Complete PHP Filesystem Reference Manual
Definition and Usage
The fgets() function returns a line from an open file.
The fgets() function stops returning a new line when the specified length (length - 1) is reached, when a newline character is encountered, or when the end of file (EOF) is reached (whichever comes first).
If it fails, the function returns FALSE.
Syntax
fgets(file,length)
| Parameters | Description |
|---|---|
| file | Required. Specifies the file to be read. |
| length | Optional. Specifies the number of bytes to read. The default is 1024 bytes. |
Example 1
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
The above code will output:
Hello, this is a test file.
Example 2: Read a File Line by Line
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
The above code will output:
Hello, this is a test file. There are three lines here. This is the last line.
Other Extensions