Latest web development tutorials

PHP fgets () function

PHP Filesystem Reference Manual Complete PHP Filesystem Reference Manual

Definition and Usage

fgets () function returns a line from an open file.

fgets () function when it reaches the end of a specified length or read file (EOF) (whichever comes first), Stop to return to a new line.

If it fails the function returns FALSE.

grammar

fgets(file,length)

参数 描述
file 必需。规定要读取的文件。
length 可选。规定要读取的字节数。默认是 1024 字节。


Example 1

<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>

The code above will output:

Hello, this is a test file.


Example 2

Read the file line by line:

<?php
$file = fopen("test.txt","r");

while(! feof($file))
{
echo fgets($file). "<br />";
}

fclose($file);
?>

The code above will output:

Hello, this is a test file.
There are three lines here.
This is the last line.


PHP Filesystem Reference Manual Complete PHP Filesystem Reference Manual