php中fgets函数用法是什么

来自:互联网
时间:2021-12-29
阅读:

php中fgets函数用法是什么

fgets() 函数从打开的文件中返回一行。

fgets() 函数会在到达指定长度( length - 1 )、碰到换行符、读到文件末尾(EOF)时(以先到者为准),停止返回一个新行。

如果失败该函数返回 FALSE。

语法为:

fgets(file,length)

其中file是必需用于规定要读取的文件,length是可选用于规定要读取的字节数。默认是 1024 字节。

示例如下:

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

或者

<?php
$file = fopen("test.txt","r");
 
while(! feof($file))
{
    echo fgets($file). "<br />";
}
 
fclose($file);
?>
返回顶部
顶部