fgets PHP función ()
Definición y Uso
la función fgets () devuelve una línea desde un archivo abierto.
la función fgets () cuando se llega al final de un período determinado o leer el archivo (EOF) (lo que ocurra primero), Stop para volver a una nueva línea.
Si falla la función devuelve FALSO.
gramática
fgets(file,length)
参数 | 描述 |
---|---|
file | 必需。规定要读取的文件。 |
length | 可选。规定要读取的字节数。默认是 1024 字节。 |
Ejemplo 1
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
El código anterior es la salida:
Hello, this is a test file.
Ejemplo 2
Lea el archivo línea por línea:
<?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);
?>
El código anterior es la salida:
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.
Completar Manual de Referencia del Sistema de Archivos PHP