PHP fgets() 函數(shù)

2018-11-21 16:04 更新

PHP fgets() 函數(shù)


PHP Filesystem 參考手冊 完整的 PHP Filesystem 參考手冊

定義和用法

fgets() 函數(shù)從打開的文件中返回一行。

fgets() 函數(shù)會在到達(dá)指定長度或讀到文件末尾(EOF)時(以先到者為準(zhǔn)),停止返回一個新行。

如果失敗該函數(shù)返回 FALSE。

語法

fgets(file,length)

參數(shù) 描述
file 必需。規(guī)定要讀取的文件。
length 可選。規(guī)定要讀取的字節(jié)數(shù)。默認(rèn)是 1024 字節(jié)。


實(shí)例 1

<?php
$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);
?>

上面的代碼將輸出:

Hello, this is a test file.


實(shí)例 2

按行讀取文件:

<?php
$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.


PHP Filesystem 參考手冊 完整的 PHP Filesystem 參考手冊
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號