欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于php fread()使用技巧

 更新時(shí)間:2010年01月22日 20:38:17   作者:  
fread() 從文件指針 handle 讀取最多 length 個(gè)字節(jié)。
說明
string fread ( int handle, int length )
fread() 從文件指針 handle 讀取最多 length 個(gè)字節(jié)。該函數(shù)在讀取完最多 length 個(gè)字節(jié)數(shù),或到達(dá) EOF 的時(shí)候,或(對于網(wǎng)絡(luò)流)當(dāng)一個(gè)包可用時(shí),或(在打開用戶空間流之后)已讀取了 8192 個(gè)字節(jié)時(shí)就會停止讀取文件,視乎先碰到哪種情況。
返回所讀取的字符串,如果出錯返回 FALSE。
復(fù)制代碼 代碼如下:

<?php
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize ($filename));
fclose($handle);
?>

警告
在區(qū)分二進(jìn)制文件和文本文件的系統(tǒng)上(如 Windows)打開文件時(shí),fopen() 函數(shù)的 mode 參數(shù)要加上 'b'。
復(fù)制代碼 代碼如下:

<?php
$filename = "c:\\files\\somepic.gif";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize ($filename));
fclose($handle);
?>

警告
當(dāng)從任何不是普通本地文件讀取時(shí),例如在讀取從遠(yuǎn)程文件或 popen() 以及 proc_open() 返回的流時(shí),讀取會在一個(gè)包可用之后停止。這意味著應(yīng)該如下例所示將數(shù)據(jù)收集起來合并成大塊。
復(fù)制代碼 代碼如下:

<?php
// 對 PHP 5 及更高版本
$handle = fopen("http://www.example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>

<?php
$handle = fopen ("http://www.example.com/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
?>

注意: 如果只是想將一個(gè)文件的內(nèi)容讀入到一個(gè)字符串中,用 file_get_contents(),它的性能比上面的代碼好得多。
額外:
file_get_contents
(PHP 4 >= 4.3.0, PHP 5)
file_get_contents -- 將整個(gè)文件讀入一個(gè)字符串
說明
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )

和 file() 一樣,只除了 file_get_contents() 把文件讀入一個(gè)字符串。將在參數(shù) offset 所指定的位置開始讀取長度為 maxlen 的內(nèi)容。如果失敗,file_get_contents() 將返回 FALSE。
file_get_contents() 函數(shù)是用來將文件的內(nèi)容讀入到一個(gè)字符串中的首選方法。如果操作系統(tǒng)支持還會使用內(nèi)存映射技術(shù)來增強(qiáng)性能。

相關(guān)文章

最新評論