PHP fgets() 函数

PHP Filesystem 函数


定义和用法

fgets() 函数从文件指针中读取一行。

语法

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

说明

file 指向的文件中读取一行并返回长度最多为 length - 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length - 1 字节后停止(要看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。

若失败,则返回 false。


提示和注释


例子

例子 1

<?php

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

?>

输出类似:

Hello, this is a test file.

例子 2

<?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.

PHP Filesystem 函数

本教程仅供参考学习,如用于商业带来的问题,本站概不负责。
关注公众号
关注公众号

©2020 IT自习室京ICP备20010815号