PHPの関数fgets()関数
定義と使用法
関数fgets()関数は、開いているファイルから行を返します。
関数fgets(どちらか早い方)には指定された長さの終わりに達するか、またはファイル(EOF)を読んで()関数、停止が新しい行に戻ります。
それが失敗した場合、関数はFALSEを返します。
文法
fgets(file,length)
参数 | 描述 |
---|---|
file | 必需。规定要读取的文件。 |
length | 可选。规定要读取的字节数。默认是 1024 字节。 |
例1
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
$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);
?>
$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.
There are three lines here.
This is the last line.
PHPファイルシステムリファレンスマニュアルを完了