轉自 https://blog.51cto.com/zhezhebie/5444041

--

 

 
<?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" )) {
$line ++ ;
}
fclose ( $fp ); //關閉文件

while ( fgets ( $getfp )) {
$lines ++ ;
}
fclose ( $getfp ); //關閉文件

printf ( " stream_get_line函數所取得的文件行數為:%s" , $line );
echo '<br>' ;
printf ( " fgets函數所取得的文件行數為:%s" , $lines ) ;

echo '<br>' ;
$end = microtime ( TRUE );
echo '耗時' . $deltime = $end - $start ;


//第二種方法
/*獲取大文件最後N行方法
原理:
首先通過fseek找到文件的最後一位EOF,然後找最後一行的起始位置,取這一行的數據,再找次一行的起始位置, 再取這一行的位置,依次類推,直到找到了$num行。
*/

$file = " F:\access_log" ;
var_dump ( tail ( $file , 10 ));

function tail ( $file , $num ) {
$fp = fopen ( $file ," r" );
$pos = - 2 ;
$eof = " " ;
$head = false ; //當總行數小於Num時,判斷是否到第一行了
$lines = array ();
while ( $num > 0 ) {
while ( $eof != " \n" ) {
if ( fseek ( $fp , $pos , SEEK_END ) == 0 ) {
//fseek成功返回0,失敗返回-1
$eof = fgetc( $fp );
$pos -- ;
} else {
//當到達第一行,行首時,設置$pos失敗
fseek ( $fp , 0 , SEEK_SET );
$head = true ; //到達文件頭部,開關打開
break ;
}
}
array_unshift ( $lines , fgets ( $fp ));
if ( $head ) { break ;} //這一句,只能放上一句後,因為到文件頭後,把第一行讀取出來再跳出整個循環
$eof = " " ;
$num --;
}
fclose ( $fp );
return $lines ;
}
?>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.

以上例子輸出結果為:
stream_get_line函數所取得的文件行數為:272717
fgets函數所取得的文件行數為:272717
耗時0.727095

看到了吧?速度還是蠻快的,兩種方法加起來耗時還不到一秒,文件一共有27萬多行!

讀取大文件內容:

 
 
function getbigfile() {
$file_path = "D:/wamp/www/test/input.txt";
$handle = fopen($file_path, "r+");
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle, 8000);
echo $buffer;
}
fclose($handle);
}
}

//getbigfile();
//die;

//返回文件從X行到Y行的內容
function getFileLine($file_path,$starline=1,$endline=5,$open_mode = "rb+"){
$fp = new SplFileObject($file_path, $open_mode);
$fp->seek($starline - 1); // 轉到第N行, seek方法參數從0開始計數
for($i=0;$i < = $endline;$i++){
$content[] = $fp- >current(); //current()獲取當前行內容
$fp->next(); // 下一行
}
return $content;
}
$file_path = "D:/wamp/www/test/input.txt";
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

 

 
 
 

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 dizzy03 的頭像
    dizzy03

    碎碎念

    dizzy03 發表在 痞客邦 留言(0) 人氣()


    留言列表 留言列表

    發表留言