fread()、fgets()、fgetc()、file_get_contents() 與 file() 函數用於從檔案讀取內容。
fread()
fread() 函數用於讀取檔案(可安全用於二進位檔案)。
語法:
1 | <span style= "color: #0000ff" >string</span> <span style= "color: #008080" > fread </span>( int handle, int length )
|
登入後複製
fread() 從檔案指標 handle 讀取最多 length 個位元組。當遇到下列任何一種情況時,會停止讀取檔案:
- 在讀取最多 length 個位元組數時
- 達到文件末尾的時候(EOF)
- (對於網路流)當一個套件可用時
- 或(在開啟用戶空間流之後)已讀取了 8192 個位元組時
從檔案讀取 10 個位元組(包括空格):
1 2 3 4 5 6 7 | <?<span style= "color: #000000" >php
</span><span style= "color: #008000" >
<span style= "color: #800080" > $filename </span> = "test.txt" <span style= "color: #000000" >;
</span><span style= "color: #800080" > $fh </span> = <span style= "color: #008080" > fopen </span>(<span style= "color: #800080" > $filename </span>, "r" <span style= "color: #000000" >);
</span><span style= "color: #0000ff" > echo </span> <span style= "color: #008080" > fread </span>(<span style= "color: #800080" > $fh </span>, "10" <span style= "color: #000000" >);
</span><span style= "color: #008080" >fclose</span>(<span style= "color: #800080" > $fh </span><span style= "color: #000000" >);
</span>?>
|
登入後複製
提示
如果只是想將一個檔案的內容讀入到一個字串中,應該用表現更好的 file_get_contents() 。
fgets()
fgets() 函數用於從檔案中讀取 一行 數據,並將檔案指標指向下一行。
提示:如果想在讀取的時候去掉檔案中的 HTML 標記,請使用 fgetss() 函數。
語法:
1 | <span style= "color: #0000ff" >string</span> <span style= "color: #008080" > fgets </span>( int handle [, int length] )
|
登入後複製
fgets() 從 handle 指向的檔案中讀取一行並傳回長度最多為 length-1 位元組的字串。碰到換行符號(包括在返回值中)、EOF 或已經讀取了 length-1 位元組後停止。如果沒有指定 length ,則預設為 1K ,或說 1024 位元組。
範例:
1 2 3 4 5 6 7 8 9 10 11 | <?<span style= "color: #000000" >php
</span><span style= "color: #800080" > $fh </span> = @<span style= "color: #008080" > fopen </span>( "test.txt" , "r" ) or <span style= "color: #0000ff" > die </span>( "打开 test.txt 文件出错!" <span style= "color: #000000" >);
</span><span style= "color: #008000" >
<span style= "color: #0000ff" > if </span>(<span style= "color: #800080" > $fh </span><span style= "color: #000000" >){
</span><span style= "color: #0000ff" > while </span>(!<span style= "color: #008080" > feof </span>(<span style= "color: #800080" > $fh </span><span style= "color: #000000" >)) {
</span><span style= "color: #0000ff" > echo </span> <span style= "color: #008080" > fgets </span>(<span style= "color: #800080" > $fh </span>), '<br />' <span style= "color: #000000" >;
}
}
</span><span style= "color: #008080" >fclose</span>(<span style= "color: #800080" > $fh </span><span style= "color: #000000" >);
</span>?>
|
登入後複製
補充說明
feof() 函數測試檔案指標是否到了檔案結束的位置,該檔案指標必須有效,如果是無效的資源,則會陷入無限循環中。參見《PHP 檔案指標函數》
fgetc()
fgetc() 函數用於 逐字 讀取文件數據,直到文件結束。
語法:
1 | <span style= "color: #0000ff" >string</span> <span style= "color: #008080" > fgetc </span>( <span style= "color: #0000ff" >resource</span> handle )
|
登入後複製
範例:
1 2 3 4 5 6 7 8 9 10 | <?<span style= "color: #000000" >php
</span><span style= "color: #800080" > $fh </span> = @<span style= "color: #008080" > fopen </span>( "test.txt" , "r" ) or <span style= "color: #0000ff" > die </span>( "打开 test.txt 文件出错!" <span style= "color: #000000" >);
</span><span style= "color: #008000" >
<span style= "color: #0000ff" > if </span>(<span style= "color: #800080" > $fh </span><span style= "color: #000000" >){
</span><span style= "color: #0000ff" > while </span>(!<span style= "color: #008080" > feof </span>(<span style= "color: #800080" > $fh </span><span style= "color: #000000" >)) {
</span><span style= "color: #0000ff" > echo </span> <span style= "color: #008080" > fgetc </span>(<span style= "color: #800080" > $fh </span><span style= "color: #000000" >);
}
}
</span><span style= "color: #008080" >fclose</span>(<span style= "color: #800080" > $fh </span><span style= "color: #000000" >);
</span>?>
|
登入後複製
file_get_contents()
file_get_contents() 函數用來把 整個檔案 讀入一個字串,成功回傳一個字串,失敗則傳回 FALSE。
語法:
1 | <span style= "color: #0000ff" >string</span> <span style= "color: #008080" > file_get_contents </span>( <span style= "color: #0000ff" >string</span> filename [, int offset [, int maxlen]] )
|
登入後複製
參數說明:
參數 說明
filename 要讀取的檔案名稱
offset 可選,並指定讀取開始的位置,預設檔案開始位置
位元組🎠
1 2 3 4 | <?<span style= "color: #000000" >php
</span><span style= "color: #008000" >
<span style= "color: #0000ff" > echo </span> <span style= "color: #008080" > nl2br </span>(<span style= "color: #008080" > file_get_contents </span>( 'test.txt' <span style= "color: #000000" >));
</span>?>
|
登入後複製
file()
file() 函數用來把 整個檔案 讀入一個陣列中,陣列中的每個單元都是檔案中對應的一行,包括換行符在內。成功傳回一個數組,失敗則傳回 FALSE。
語法:
1 | <span style= "color: #0000ff" > array </span> <span style= "color: #008080" >file</span>( <span style= "color: #0000ff" >string</span> filename )
|
登入後複製
範例:
1 2 3 4 5 6 7 8 | <?<span style= "color: #000000" >php
</span><span style= "color: #800080" > $lines </span> = <span style= "color: #008080" >file</span>( 'test.txt' <span style= "color: #000000" >);
</span><span style= "color: #008000" >
<span style= "color: #0000ff" > foreach </span> (<span style= "color: #800080" > $lines </span> <span style= "color: #0000ff" > as </span> <span style= "color: #800080" > $line_num </span> => <span style= "color: #800080" > $line </span><span style= "color: #000000" >) {
</span><span style= "color: #0000ff" > echo </span> "Line #{<span style=" color: #800080 ">$line_num</span>} : " ,<span style= "color: #800080" > $line </span>, '<br />' <span style= "color: #000000" >;
}
</span>?>
|
登入後複製
test.txt 檔案內容:
你好!
這是第二行文字。
瀏覽器顯示:
Line #0 : 你好!
Line #1 : 這是第二行文字。