função fgets () do PHP
Definição e Uso
função fgets () retorna uma linha de um arquivo aberto.
fgets () função quando ela chega ao fim de um determinado período ou ler o arquivo (EOF) (o que ocorrer primeiro), Parar para retornar para uma nova linha.
Se falhar a função retorna FALSE.
gramática
fgets(file,length)
参数 | 描述 |
---|---|
file | 必需。规定要读取的文件。 |
length | 可选。规定要读取的字节数。默认是 1024 字节。 |
exemplo 1
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
O código acima exibirá:
Hello, this is a test file.
exemplo 2
Leia o arquivo linha por linha:
<?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);
?>
O código acima exibirá:
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.
Manual Completo PHP Sistema de Arquivos de Referência