php使用file-get-contents讀取大文件的方法
本文介紹了在php中使用file_get_contents函數讀取大文件的方法,並附上了示例以及使用小技巧,非常的實用,這裏推薦給大家,就跟隨本站小編一起去了解下吧,想了解更多相關信息請持續關注我們應屆畢業生考試網!
當我們遇到文本文件體積很大時,比如超過幾十M甚至幾百M幾G的大文件,用記事本或者其它編輯器打開往往不能成功,因爲他們都需要把文件內容全部放到內存裏面,這時就會發生內存溢出而打開錯誤,遇到這種情況我們可以使用PHP的文件讀取函數file_get_contents()進行分段讀取。
函數說明
string file_get_contents ( string $filename [, bool $use_include_path [, resource $context [, int $offset [, int $maxlen ]]]] )
和 file() 一樣,只除了 file_get_contents() 把文件讀入一個字符串。將在參數 offset 所指定的`位置開始讀取長度爲 maxlen 的內容。如果失敗,file_get_contents() 將返回 FALSE。
file_get_contents() 函數是用來將文件的內容讀入到一個字符串中的首選方法。如果操作系統支持還會使用內存映射技術來增強性能。
應用:
複製代碼 代碼如下:
$str = $content=file_get_contents("",FALSE,NULL,1024*1024,1024);
echo $str;
如果針對較小文件只是希望分段讀取並以此讀完可以使用fread()函數
複製代碼 代碼如下:
$fp=fopen('','r');
while (!feof($fp)){
$str.=fread($fp, filesize ($filename)/10);//每次讀出文件10分之1
//進行處理
}
echo $str;
以上就是如何使用file_get_contents函數讀取大文件的方法,超級簡單吧,需要的小夥伴直接搬走!
相關文章
-
解決PHP中file-get-contents函數的方法
方法一:在php中,抓取https的`網站,提示如下的錯誤內容:Waing: file_get_contents() [-get-contents]: failed to open stream: Invalid argument in I: on line 16打開文件找到 ;extension=php_ ,去掉雙引號”;” ,重啓web -
The Best Thing That Could Ever Happen少兒雙語閱讀
Unit 3 The Best Thing That Could Ever Happen單元 3 最棒的事Three old men were sitting around and talking.三個老頭坐在一起聊天。The 80-year-old remarked, "The best thing that could happen to me would j -
php的file-get-contents與curl性能分析
主要介紹了php中file_get_contents與curl性能比較,以實例形式詳細分析了file_get_contents與curl的區別以及運行效率的對比,需要的朋友可以參考下.就跟隨本站小編一起去了解下吧,想了解更多相關信息請持續關注我們應屆 -
php中file-get-contents與curl性能分析
PHP,是英文超文本預處理語言Hypertext Preprocessor的縮寫。下面小編帶大家學習一下php中file_get_contents與curl性能分析,歡迎閱讀,更多詳情請關注應屆畢業生考試網。在php中如果不仔細的去分析性能會發現file_get_con -
php中file-get-contents與curl性能比較
服裝網絡營銷工作計劃引言在我國服裝營銷這一領域的理論研究與應用剛剛起步,本論文的目的是從宏觀的角度討論整個服裝業目前的發展,再推及到杭州魅力女裝網絡營銷活動方案,挖掘在服裝行業各個層次的內在關需求和潛在市場 -
php中file-get-contents與curl性能比較分析
在php中如果不仔細的去分析性能會發現file_get_contents與curl兩個同很多共同點的,他們都可以採集文件打開文件,但是如果仔細一對比會發現很多不同點,以下是小編爲大家搜索整理的php中file_get_contents與curl性能比較分 -
php中file-get-contents與curl性能比較分析的內容
PHP中fopen,file_get_contents,curl函數的區別:n /file_get_contents 每次請求都會重新做DNS查詢,並不對 DNS信息進行緩存。但是CURL會自動對DNS信息進行緩存。對同一域名下的網頁或者圖片的`請求只需要一次DNS查詢。這 -
The place of Science and Technology in Modern Life英語作文
human life can not continue without science and technology. for many years, human society has developed with the advance of science and technology while the development of science and technology has i -
英語高分作文:The importance of protecting the environment
Dear Editor,In the past, my hometown used to be a beautiful place. It was surrounded by plenty of trees and covered with green grass everywhere. Later, people destroyed many trees to tu for its into f -
Ought Parents to Give Children Pocket英語作文
We live in a commercial society. Everything that we use is merchandise. In the commercial world the media is money. Therefore, money is very important to us. Eaing money and spending money are big pro