fgets() 函数从文件指针中读取一行。
fgets(file,length)
参数 | 描述 |
---|---|
file | 必需。规定要读取的文件。 |
length | 可选。规定要读取的字节数。默认是 1024 字节。 |
从 file 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(要看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。
若失败,则返回 false。
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
输出类似:
Hello, this is a test file.
<?php
$file = fopen("test.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
输出类似:
Hello, this is a test file.
There are three lines here.
This is the last line.
©2020 IT自习室京ICP备20010815号