PHP fgets() Function
❮ PHP Filesystem ReferenceExample
Read one line from the open file:
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
Run Example »
Definition and Usage
The fgets() function returns a line from an open file.
Syntax
fgets(file, length)
Parameter Values
Parameter | Description |
---|---|
file | Required. Specifies the open file to return a line from |
length | Optional. Specifies the number of bytes to read. Reading stops when length-1 bytes have been reached, or when a new line occurs, or on EOF. If no length is specified, it reads until end of the line |
Technical Details
Return Value: | A single line read from the file on success, FALSE on EOF or error |
---|---|
PHP Version: | 4.0+ |
Binary Safe: | Yes, in PHP 4.3 |
More Examples
Example
Read open file, line by line:
<?php
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
Run Example »
❮ PHP Filesystem Reference