PHP fgets() 函數


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


定義和用法

fgets() 函數從打開的檔中返回一行。

fgets() 函數會在到達指定長度( length - 1 )、碰到換行符、讀到檔末尾(EOF)時(以先到者為准),停止返回一個新行。

如果失敗該函數返回 FALSE。

語法

fgets(file,length)

參數 描述
file 必需。規定要讀取的檔。
length 可選。規定要讀取的位元組數。默認是 1024 位元組。


實例 1

<?php $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); ?>

上面的代碼將輸出:

Hello, this is a test file.
There are three lines here.
This is the last line.


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