<?php
$start
=microtime
(true);
$filesize
=filesize
('shuiping_yang.log');
$fp=
fopen
('shuiping_yang.log'
,'r');
$getfp
=fopen
('shuiping_yang.log'
,'r');
$lines=0
;$line=0
;//獲取檔案的一行內容,注意:需要php5才支援該函式;
//第一種方法,可以設定定界符"\r \t \n",注意設定定界符的時候要用雙引號!
while
(stream_get_line
($fp
,$filesize
,"\n"))
fclose
($fp);
//關閉檔案
while
(fgets
($getfp))
fclose
($getfp);
//關閉檔案
printf
("stream_get_line函式所取得的檔案行數為:%s"
,$line);
echo''
;printf
("fgets函式所取得的檔案行數為:%s"
,$lines);
echo''
;$end
=microtime
(true);
echo
'耗時'
.$deltime
=$end
-$start
;//第二種方法
/*獲取大檔案最後n行方法
原理:首先通過fseek找到檔案的最後一位eof,然後找最後一行的起始位置,取這一行的資料,再找次一行的起始位置, 再取這一行的位置,依次類推,直到找到了$num行。
*/$file
="f:\access_log"
;var_dump
(tail
($file,10
));function
tail
($file
,$num
)else
}array_unshift
($lines
,fgets
($fp))
;if($head
)//這一句,只能放上一句後,因為到檔案頭後,把第一行讀取出來再跳出整個迴圈
$eof=""
;$num--;
}fclose
($fp);
return
$lines;}
?>
以上例子輸出結果為:
stream_get_line函式所取得的檔案行數為:272717
fgets函式所取得的檔案行數為:272717
耗時0.727095
看到了吧?速度還是蠻快的,兩種方法加起來耗時還不到一秒,檔案一共有27萬多行!!!!
function
getbigfile()
fclose
($handle);
}}//getbigfile();
//die;
//返回檔案從x行到y行的內容
function
getfileline
($file_path
,$starline=1
,$endline=5
,$open_mode
="rb+"
)return
$content;}
$file_path
="d:/wamp/www/test/input.txt"
;
php 讀取超大檔案
php開發很多時候都要讀取大檔案,比如csv檔案 text檔案等。這些檔案如果很大,比如10個g。這時,直接一次性把所有的內容讀取到記憶體中計算不太現實。遇到這種情況,往往覺得php太弱,實則不然。利用生成器 關鍵字yield 就能解決。好了,上 created by phpstorm.user a...
python獲取大檔案行數
背景 處理一些日誌或者請求資料時,幾百萬行的資料,有時候在做效能測試任務時估算出大概需要的詞表數,需要一定行數的資料,需要提前看下原始檔案大小,在這記一下,的確比較快 如下 獲取檔案行數,一塊一塊讀取 def get file lines filepath with open filepath,rb...
php讀取超大檔案fseek
function readmaxfile fp start 0 fseek fp,start,seek set res fread fp,1 content res if substr content,strlen tag tag start 1 if feof fp sleep 3 echo 從位...