fgets PHP function ()
Definicja i Wykorzystanie
Funkcja fgets () zwraca wiersz z otwartego pliku.
Funkcja fgets (), gdy dojdzie do końca określonej długości lub odczytać pliku (EOF) (w zależności od tego co nastąpi wcześniej), Stop, aby powrócić do nowej linii.
Jeśli to się nie powiedzie funkcja zwraca FAŁSZ.
gramatyka
fgets(file,length)
参数 | 描述 |
---|---|
file | 必需。规定要读取的文件。 |
length | 可选。规定要读取的字节数。默认是 1024 字节。 |
Przykład 1
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
Powyższy kod wyjścia będzie:
Hello, this is a test file.
Przykład 2
Czytaj pliku wiersz po wierszu:
<?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);
?>
Powyższy kod wyjścia będzie:
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.
Kompletna instrukcja Reference System plików PHP