php中file_get_contents如何讀取大容量文件
一、總結
一句話總結:使用file_get_contents()進行分段讀取,file_get_contents()函數可以分段讀取
1、讀取大文件是,file_get_contents()函數為什么會發生錯誤?
發生內存溢出而打開錯誤
當我們遇到文本文件體積很大時,比如超過幾十M甚至幾百M幾G的大文件,用記事本或者其它編輯器打開往往不能成功,因為他們都需要把文件內容全部放到內存里面,這時就會發生內存溢出而打開錯誤
因為file_get_contents()只能讀取長度為 maxlen 的內容
2、file_get_contents()函數的機制是什么?
file_get_contents() 把文件讀入一個字符串。將在參數 offset 所指定的位置開始讀取長度為 maxlen 的內容。如果失敗,file_get_contents() 將返回 FALSE。
3、file_get_contents()函數如何讀取大文件?
使用file_get_contents()進行分段讀取
$str = $content=file_get_contents("2.sql",FALSE,NULL,1024*1024,1024);
echo $str;
$u ='www.bKjia.c0m'; //此文件為100GB
$a =file_get_contents( $u,100,1000 );
讀取成功了
4、fread()函數如何分段讀取?
其實就是判斷文件是否結束,沒結束就一直讀
$fp=fopen('2.sql','r');
while (!feof($fp)){
$str.=fread($fp, filesize ($filename)/10);//每次讀出文件10分之1
5、如何設置file_get_contents
函數的超時時間?
<?php //設置超時參數 $opts=array( "http"=>array( "method"=>"GET", "timeout"=>3 ), ); ////創建數據流上下文 $context = stream_context_create($opts); //$url請求的地址,例如: $result =file_get_contents($url, false, $context); // 打印結果 print_r($result); ?>
二、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("2.sql",FALSE,NULL,1024*1024,1024);
echo $str;
如果針對較小文件只是希望分段讀取並以此讀完可以使用fread()函數
$fp=fopen('2.sql','r');
while (!feof($fp)){
$str.=fread($fp, filesize ($filename)/10);//每次讀出文件10分之1
//進行處理
}
echo $str;
以上就是如何使用file_get_contents函數讀取大文件的方法,超級簡單吧,需要的小伙伴直接搬走!
參考:php 使用file_get_contents讀取大文件的方法_php技巧_腳本之家
https://www.jb51.net/article/57380.htm
三、解決php中file_get_contents 讀取大文件返回false問題
file_get_contents文件是用來讀寫文件的,但我發現用file_get_contents 讀取大文件出錯提示Note: string can be as large as 2GB了,這個就是不能超過2G了,有沒有辦法解決呢,下面我來一起來看。
如果我讀取一個 www.bKjia.c0m文件
代碼如下
復制代碼
$u ='www.bKjia.c0m'; //此文件為100GB
$a =file_get_contents( $u );
運行提示
Note: string can be as large as 2GB
不能大於2GB了,我們去官方看此函數參考
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
發現有個
file_get_contents() 把文件讀入一個字符串。將在參數 offset 所指定的位置開始讀取長度為 maxlen 的內容。如果失敗, file_get_contents() 將返回 FALSE。
原來如此,這樣我們對程序進行修改即可
代碼如下
復制代碼
$u ='www.bKjia.c0m'; //此文件為100GB
$a =file_get_contents( $u,100,1000 );
讀取成功了
總結
file_get_contents如果正常返回,會把文件內容儲存到某個字符串中,所以它不應該返回超過2G長度的字符串。
如果文件內容超過2G,不加offset和maxlen調用file_get_contents的話,肯定會返回false,
參考:解決php中file_get_contents 讀取大文件返回false問題 | E網新時代
http://www.jxtobo.com/56854.html
四、PHP中file_get_contents($url)的超時處理
PHP中file_get_contents
函數的作用是獲取一個 URL 的返回內容。如果是url響應速度慢,或者網絡等因素,會造成等待時間較長的情況。只需設置一下file_get_contents
函數的超時時間即可解決。示例代碼如下:
<?php //設置超時參數 $opts=array( "http"=>array( "method"=>"GET", "timeout"=>3 ), ); ////創建數據流上下文 $context = stream_context_create($opts); //$url請求的地址,例如: $result =file_get_contents($url, false, $context); // 打印結果 print_r($result); ?>
參考:PHP中file_get_contents($url)的超時處理 - CSDN博客
https://blog.csdn.net/mayuko2012/article/details/76092312