PHP fgets () function
Definisi dan Penggunaan
fgets () mengembalikan fungsi garis dari file terbuka.
fgets () fungsi saat mencapai akhir dari panjang tertentu atau membaca file (EOF) (mana yang lebih dulu), Berhenti untuk kembali ke baris baru.
Jika gagal fungsi mengembalikan FALSE.
tatabahasa
fgets(file,length)
参数 | 描述 |
---|---|
file | 必需。规定要读取的文件。 |
length | 可选。规定要读取的字节数。默认是 1024 字节。 |
contoh 1
<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>
Kode di atas akan menampilkan:
Hello, this is a test file.
contoh 2
Membaca file baris demi baris:
<?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);
?>
Kode di atas akan menampilkan:
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.
Lengkapi manual PHP Filesystem Referensi