PHP fread() 函數

定義和用法
fread() 函數讀取打開的檔。
函數會在到達指定長度或讀到檔末尾(EOF)時(以先到者為准),停止運行。
該函數返回讀取的字串,如果失敗則返回 FALSE。
語法
string fread ( resource $handle , int $length )
參數 | 描述 |
---|---|
handle | 檔系統指針,是典型地由 fopen() 創建的 resource(資源)。 |
length | 必需。規定要讀取的最大字節數。 |
提示和注釋
提示:該函數是二進位安全的。(意思是二進位數據(如圖像)和字元數據都可以使用此函數寫入。)
實例 1
從檔中讀取 10 個位元組:
<?php
$file = fopen("test.txt","r");
$contents = fread($file,"10");
fclose($file);
?>
$file = fopen("test.txt","r");
$contents = fread($file,"10");
fclose($file);
?>
實例 2
讀取整個檔:
<?php
$file = fopen("test.txt","r");
$contents = fread($file,filesize("test.txt"));
fclose($file);
?>
$file = fopen("test.txt","r");
$contents = fread($file,filesize("test.txt"));
fclose($file);
?>
