fgets PHP () ฟังก์ชัน
ความหมายและการใช้งาน
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 อ้างอิง