您好,登錄后才能下訂單哦!
php中如何讀取大文件,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
Lib_File2.php
代碼如下:
<?php
class Lib_File2
{
//文件目錄
private $root = '/data/wwwroot/kkpromo/data/';
//文件后綴
private $suffix = '.log';
//文件句柄
private $handle=null;
//一次讀取文件的最大記錄數(shù)
private $limit=40000;
//每行讀取的字節(jié)長度
private $length=1024;
//開始時間
private $startTime=0;
//內(nèi)存使用基準(zhǔn)點
private static $startMemory=0;
//
private $conn=null;
//
private static $init=null;
public static function instance()
{
self::$startMemory = memory_get_usage(true);
if(self::$init && is_object(self::$init))
{
return self::$init;
}
self::$init = new self();
return self::$init;
}
private function __construct(){}
public function setRoot($root)
{
if(!is_dir($root)) die($root.' ROOT DOES NOT EXIST');
$this->root = $root;
}
public function setSuffix($suffix)
{
$this->suffix = $suffix;
}
public function setLimit($limit)
{
if(!is_numeric($limit)) die($limit.' SHOULD BE NUMBERIC');
if(intval($limit) > 1000000) die($limit.' SHOULD BE LOWER THAN 1000000');
$this->limit = intval($limit);
}
public function _getFile($date , $appid , $op)
{
$filename = rtrim($this->root , '/').DIRECTORY_SEPARATOR.$date.DIRECTORY_SEPARATOR.$appid.'.'.$op.$this->suffix;
if(!file_exists($filename))
{
die($filename.' FILE DOES NOT EXISTS!');
}
if(!is_file($filename))
{
die($filename.' FILE DOES NOT EXIST!');
}
if(!is_readable($filename))
{
die($filename.' FILE ACCESS DENY!');
}
return $filename;
}
public function closeFile($date=null , $appid=null , $op=null)
{
if($op && $date && $appid)
{
if(is_object($this->handle[$date.'_'.$appid.'_'.$op]) || $this->conn[$date.'_'.$appid.'_'.$op])
{
unset($this->handle[$date.'_'.$appid.'_'.$op]);
$this->handle[$date.'_'.$appid.'_'.$op]=null;
}
$this->conn[$date.'_'.$appid.'_'.$op]=null;
$this->handle[$date.'_'.$appid.'_'.$op]=null;
unset($this->handle[$date.'_'.$appid.'_'.$op]);
}
else {
if(is_array($this->handle) && $this->handle)
{
foreach ($this->handle as $key=>$val){
unset($this->handle[$key]);
$this->conn[$key]=null;
$this->handle[$key]=null;
}
}
}
return true;
}
private function _openFile($date , $appid , $op)
{
$this->startTime = microtime(true);
if(isset($this->conn[$date.'_'.$appid.'_'.$op]) && $this->conn[$date.'_'.$appid.'_'.$op])
{
return $this->handle[$date.'_'.$appid.'_'.$op];
}
$filename = self::_getFile($date , $appid , $op);
if(($this->handle[$date.'_'.$appid.'_'.$op] = new SplFileObject($filename , 'r'))!=null)
{
$this->conn[$date.'_'.$appid.'_'.$op] = true;
return $this->handle[$date.'_'.$appid.'_'.$op];
}
else {
die('FILE OPEN FAILED!');
}
}
/**
* 功能:解析數(shù)據(jù)
* 格式: array('timestamp','mid','data');
* @param string $data
* @return boolean|array
*/
private function _parseData($data , $jsonFlag=true)
{
if(empty($data) || !is_string($data)) return false;
$result = array(
'timestamp'=>0,
'mid'=>0,
'data'=>array(),
);
$data = explode('|', $data);
if(count($data) < 3 || !is_array($data)) return false;
$result['timestamp'] = $data[0];
$result['mid'] = $data[1];
if($jsonFlag)
{
$result['data'] = @json_decode($data[2] , true);
unset($result['mid']);
}
if(empty($result['timestamp']) || empty($result['mid'])) return false;
unset($data);
return $result;
}
/**
* TODO:讀取單一文件
* @param string $date: 如(20140327)
* @param int $appid: 如(1000,9000)
* @param string $op:如(show,login , index)
* @param number $startNum 默認(rèn)從第一行開始
* @param number $length 默認(rèn)到$this->limit 讀取的行數(shù)
* @param array $condition:array('mid'=>arrray() , 'ip'=>array() , ...) 過濾條件
* @param bool $jsonFlag:默認(rèn)為true, 則保留jsondata字段;設(shè)為false,則去掉false字段
* @return array(count , diffTime , memory , data)
*/
public function readFile($date , $appid , $op , $startNum=0 , $length=0 , $jsonFlag=false , $condition=array())
{
$data['data'] = "";
$data['count'] = 0;
$index = $startNum;
$startNum = empty($startNum) ? 0 : $startNum;
$length = empty($length) ? $this->limit : $length;
$handle = self::_openFile($date , $appid , $op );
$line_number=0;
if($handle)
{
$handle->seek($startNum);
$handle->setMaxLineLen($this->length);
while (intval($line_number) - intval($startNum) < intval($length)-1)
{
$tmp = $handle->current();
if(empty($tmp)) continue;
$tmp = self::_parseData($tmp , $jsonFlag);
$line_number = $handle->key();
!$jsonFlag && $condition= array();
if(isset($condition) && $condition)
{
$key = array_keys($condition);
if(in_array($tmp['data'][$key[0]], $condition[$key[0]]))
{
$data['count']++;
$data['data'][$line_number] = $tmp;
}
}
else
{
$data['data'][$line_number] = $tmp;
$data['count']++;
}
if(intval($line_number) - intval($startNum) >= intval($length)-1) break;
unset($tmp);
$handle->next();
}
unset($tmp , $length , $line_number , $condition);
}
$data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$data['memory'] = doubleval((doubleval(memory_get_usage(true)) - doubleval(self::$startMemory))/1024/1024) . ' M';
return $data;
}
/**
* TODO:命令行下獲取文件總記錄數(shù)*
* @param string $date
* @param int $appid
* @param string $op
* @return array
*/
public function total_lineFile($date, $appid, $op)
{
$this->_openFile($date, $appid, $op);
$file = escapeshellarg($this->_getFile($date, $appid, $op)); // 對命令行參數(shù)進(jìn)行安全轉(zhuǎn)義
$line = `wc -l $file`;
if(preg_match("/(\d{1,})/", $line , $ret)){
$data['count']=$ret[1];
}else{
$data['count']=0;
}
$data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$data['memory'] = doubleval((doubleval(memory_get_usage(true)) - doubleval(self::$startMemory))/1024/1024) . ' M';
return $data;
}
/**
* TODO:統(tǒng)計{$data}.{$op}.log記錄數(shù)
* @param string $date
* @param int $appid
* @param string $op
* @param array $condition
* @return array
*/
public function countFile($date , $appid , $op ,$condition=array())
{
$data['count'] = 0;
$handle = self::_openFile($date , $appid , $op );
if($handle)
{
$handle->setMaxLineLen($this->length);
while (!$handle->eof())
{
$tmp = $handle->current();
if(empty($tmp)) continue;
$tmp = self::_parseData($tmp);
if($condition && is_array($condition) )
{
$key = array_keys($condition);
if(isset($tmp['data'][$key[0]]) && $tmp['data'][$key[0]] && in_array($tmp['data'][$key[0]], $condition[$key[0]]))
{
$data['count']++;
}
}
else
{
$data['count']++;
}
unset($tmp);
$handle->next();
}
}
unset($handle , $condition , $tmp , $key , $val);
self::closeFile($date , $appid , $op );
$data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$data['memory'] = doubleval((doubleval(memory_get_usage(true)) - doubleval(self::$startMemory))/1024/1024) . ' M';
return $data;
}
/**
* TODO:統(tǒng)計用戶數(shù)
* @param string $date
* @param int $appid
* @param string $op
* @param bool $midflag :默認(rèn)為false 則 mid返回空數(shù)組;如設(shè)為true,則mid數(shù)組不為空
* * @param bool $jsonFlag:默認(rèn)為true, 則保留jsondata字段;設(shè)為false,則去掉jsondata字段
* @param array $condition
* @return : array:形如({"mid":[],"count":2181,"diffTime":0.0397667884827,"memory":"3.75 M"})
*/
public function countFileMID($date , $appid , $op , $midflag=false , $jsonFlag=false, $condition=array())
{
//$count = self::total_lineFile($date , $appid , $op );
$count = self::countFile($date , $appid , $op );
$index = ceil($count['count'] / $this->limit);
$result = array('mid'=>array() , 'count'=>0 , 'diffTime'=>0 , 'memory'=>0);
for ($i=0 ; $i<$index ; $i++)
{
$startNum = $this->limit*$i;
$endNum = $this->limit;
$data = self::readFile($date , $appid , $op , $startNum , $endNum , $jsonFlag);
var_dump($data);exit();
if($data['data'] && is_array($data['data']))
{
foreach ($data['data'] as $arr)
{
if($condition && is_array($condition))
{
$key = array_keys($condition);
if(isset($arr['data'][$key[0]]) && (in_array($arr['data'][$key[0]] , $condition[$key]) || empty($condition[$key[0]])))
{
$result['mid'][$arr["mid"]] =1;
$result['count']++;
}
}
else
{
$result['mid'][$arr["mid"]] =1;
$result['count']++;
}
unset($data);
}
}
}
unset($index , $count , $condition , $data , $arr);
self::closeFile($date , $appid , $op);
$result['mid'] = array_keys($result['mid']);
if(empty($midflag)) unset($result['mid']);
$result['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$result['memory'] = (memory_get_usage(true) - self::$startMemory)/1024/1024 . ' M';
return $result;
}
/**
* TODO:跨時間段 統(tǒng)計參加$op用戶數(shù)據(jù)
* @param string $date
* @param int $appid
* @param string $op
* @param number $day
* @param bool $midflag :默認(rèn)為false 則 mid返回空數(shù)組;如設(shè)為true,則mid數(shù)組不為空
* @return array 形如("20140326":{"mid":[],"count":4571,"diffTime":0.0806441307068,"memory":"3.75 M"},
* "20140325":{"mid":[],"count":2181,"diffTime":0.0397667884827,"memory":"3.75 M"})
*/
public function getReturnUser($date , $appid , $op , $day=1 , $midflag=false)
{
$date_i=0;
for ($i =0; $i<$day ; $i++){
$date_i = date('Ymd' , strtotime($date)-$i*86400);
$result[$date_i] = self::countFileMID($date_i , $appid , $op , $midflag);
}
unset($date , $date_i , $appid , $op , $day);
return $result;
}
}
?>
Lib_File1.php
復(fù)制代碼 代碼如下:
<?php
class Lib_File1
{
//文件目錄
private $root = '/data/wwwroot/kkpromo/data/';
//文件后綴
private $suffix = '.log';
//文件句柄
private $hander=null;
//一次讀取文件的最大記錄數(shù)
private $limit=40000;
//每行讀取的字節(jié)長度
private $length=1024;
//開始時間
private $startTime=0;
//內(nèi)存使用基準(zhǔn)點
private static $startMemory=0;
//
private $conn=null;
//
private static $init=null;
public static function instance()
{
self::$startMemory = memory_get_usage(true);
if(self::$init && is_object(self::$init))
{
return self::$init;
}
self::$init = new self();
return self::$init;
}
private function __construct(){}
public function setRoot($root)
{
if(!is_dir($root)) die($root.' ROOT DOES NOT EXIST');
$this->root = $root;
}
public function setSuffix($suffix)
{
$this->suffix = $suffix;
}
public function setLimit($limit)
{
if(!is_numeric($limit)) die($limit.' SHOULD BE NUMBERIC');
if(intval($limit) > 1000000) die($limit.' SHOULD BE LOWER THAN 1000000');
$this->limit = intval($limit);
}
private function _getFile($date , $appid , $op)
{
$filename = rtrim($this->root , '/').DIRECTORY_SEPARATOR.$date.DIRECTORY_SEPARATOR.$appid.'.'.$op.$this->suffix;
if(!file_exists($filename))
{
die($filename.' FILE DOES NOT EXISTS!');
}
if(!is_file($filename))
{
die($filename.' FILE DOES NOT EXIST!');
}
if(!is_readable($filename))
{
die($filename.' FILE ACCESS DENY!');
}
return $filename;
}
public function closeFile($date=null , $appid=null , $op=null)
{
if($op && $date && $appid)
{
if(is_object($this->hander[$date.'_'.$appid.'_'.$op]) || $this->conn[$date.'_'.$appid.'_'.$op])
{
fclose($this->hander[$date.'_'.$appid.'_'.$op]);
}
$this->conn[$date.'_'.$appid.'_'.$op]=null;
$this->hander[$date.'_'.$appid.'_'.$op]=null;
}
else {
if(is_array($this->hander) && $this->hander)
{
foreach ($this->hander as $key=>$val){
fclose($this->hander[$key]);
$this->conn[$key]=null;
$this->hander[$key]=null;
}
}
}
return true;
}
private function _openFile($date , $appid , $op)
{
$this->startTime = microtime(true);
if(isset($this->conn[$date.'_'.$appid.'_'.$op]) && $this->conn[$date.'_'.$appid.'_'.$op])
{
return $this->hander[$date.'_'.$appid.'_'.$op];
}
$filename = self::_getFile($date , $appid , $op);
if(($this->hander[$date.'_'.$appid.'_'.$op] = fopen($filename, 'r'))!=null)
{
$this->conn[$date.'_'.$appid.'_'.$op] = true;
return $this->hander[$date.'_'.$appid.'_'.$op];
}
else {
die('FILE OPEN FAILED!');
}
}
/**
* 功能:解析數(shù)據(jù)
* 格式: array('timestamp','mid','data');
* @param string $data
* @return boolean|array
*/
private function _parseData($data)
{
if(empty($data) || !is_string($data)) return false;
$result = array(
'timestamp'=>0,
'mid'=>0,
'data'=>array(),
);
$data = explode('|', $data);
if(count($data) < 3 || !is_array($data)) return false;
$result['timestamp'] = $data[0];
$result['mid'] = $data[1];
$result['data'] = @json_decode($data[2] , true);
if(empty($result['timestamp']) || empty($result['mid'])) return false;
unset($data);
return $result;
}
/**
* TODO:讀取單一文件
* @param string $date: 如(20140327)
* @param int $appid: 如(1000,9000)
* @param string $op:如(show,login , index)
* @param number $startNum 默認(rèn)從第一行開始
* @param number $endNum 默認(rèn)到$this->limit結(jié)束
* @param array $condition:array('mid'=>arrray() , 'ip'=>array() , ...) 過濾條件
* @param bool $jsonFlag:默認(rèn)為true, 則保留jsondata字段;設(shè)為false,則去掉false字段
* @return array(count , diffTime , memory , data)
*/
public function readFile($date , $appid , $op ,$startNum=0 , $endNum=0 , $jsonFlag=false , $condition=array())
{
$data['data'] = "";
$data['count'] = 0;
$index = $startNum;
$startNum = empty($startNum) ? 0 : $startNum;
$endNum = empty($endNum) ? $this->limit : $endNum;
$hander = self::_openFile($date , $appid , $op );
$tmpindex=0;
if($hander)
{
//!feof($hander)
while ($tmpindex < $endNum)
{
$tmp = fgets($hander , $this->length);
if(empty($tmp)) continue;
if($tmpindex < $endNum && $tmpindex >=$startNum)
{
$tmp = self::_parseData($tmp);
if(empty($tmp)) continue;
//去掉jsondata
if(!$jsonFlag) { unset($tmp[2]); $condition= array(); }
//條件過濾
if($condition && is_array($condition) )
{
foreach ($condition as $key=>$val){
if(in_array($tmp['data'][$key], $condition[$key]))
unset($key , $val);
$data['count']++;
$data['data'][$index] = $tmp;
$index++;
}
}
else{
$data['data'][$index] = $tmp;
$index++;
$data['count']++;
}
}
if($tmpindex >= $endNum) break;
$tmpindex++;
unset($tmp);
}
fseek($hander , SEEK_END);
}
$data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$data['memory'] = doubleval((doubleval(memory_get_usage(true)) - doubleval(self::$startMemory))/1024/1024) . ' M';
return $data;
}
/**
* TODO:命令行下獲取文件總記錄數(shù)*
* @param string $date
* @param int $appid
* @param string $op
* @return array
*/
public function total_lineFile($date, $appid, $op)
{
$this->_openFile($date, $appid, $op);
$file = escapeshellarg($this->_getFile($date, $appid, $op)); // 對命令行參數(shù)進(jìn)行安全轉(zhuǎn)義
$line = `wc -l $file`;
if(preg_match("/(\d{1,})/", $line , $ret)){
$data['count']=$ret[1];
}else{
$data['count']=0;
}
$data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$data['memory'] = doubleval((doubleval(memory_get_usage(true)) - doubleval(self::$startMemory))/1024/1024) . ' M';
return $data;
}
/**
* TODO:統(tǒng)計{$data}.{$op}.log記錄數(shù)
* @param string $date
* @param int $appid
* @param string $op
* @param array $condition
* @return array
*/
public function countFile($date , $appid , $op ,$condition=array())
{
$data['count'] = 0;
$hander = self::_openFile($date , $appid , $op );
if($hander)
{
while (!feof($hander))
{
$tmp = fgets($hander , $this->length);
$tmp = self::_parseData($tmp);
if(empty($tmp)) continue;
if($condition && is_array($condition) )
{
foreach ($condition as $key=>$val){
if(isset($tmp['data'][$key]) && $tmp['data'][$key] && in_array($tmp['data'][$key], $condition[$key])){
unset($key , $val);
$data['count']++;
}
}
}
else
$data['count']++;
unset($tmp);
}
fseek($hander , SEEK_END);
}
$data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$data['memory'] = doubleval((doubleval(memory_get_usage(true)) - doubleval(self::$startMemory))/1024/1024) . ' M';
return $data;
}
/**
* TODO:統(tǒng)計用戶數(shù)
* @param string $date
* @param int $appid
* @param string $op
* @param bool $midflag :默認(rèn)為false 則 mid返回空數(shù)組;如設(shè)為true,則mid數(shù)組不為空
* * @param bool $jsonFlag:默認(rèn)為true, 則保留jsondata字段;設(shè)為false,則去掉jsondata字段
* @param array $condition
* @return : array:形如({"mid":[],"count":2181,"diffTime":0.0397667884827,"memory":"3.75 M"})
*/
public function countFileMID($date , $appid , $op , $midflag=false , $jsonFlag=false, $condition=array())
{
$count = self::total_lineFile($date , $appid , $op );
$index = ceil($count['count'] / $this->limit);
$result = array('mid'=>array() , 'count'=>0 , 'diffTime'=>0 , 'memory'=>0);
for ($i=0 ; $i<$index ; $i++)
{
$startNum = $this->limit*$i;
$endNum = $this->limit*($i+1);
$data = self::readFile($date , $appid , $op , $startNum , $endNum , $jsonFlag);
if($data['data'] && is_array($data['data']))
{
foreach ($data['data'] as $arr)
{
if($condition && is_array($condition)){
foreach ($condition as $key=>$val){
if(isset($arr['data'][$key]) && (in_array($arr['data'][$key] , $condition[$key]) || empty($condition[$key]))){
if(!isset($result['mid'][$arr['mid']])) { $result['mid'][$arr["mid"]] =1; $result['count']++; }
}
}
}
else {
if(!isset($result['mid'][$arr['mid']])) { $result['mid'][$arr["mid"]] =1; $result['count']++; }
}
}
}
unset($data['data'] , $data);
}
unset($index , $count , $condition , $data , $arr);
self::closeFile($date , $appid , $op);
$result['mid'] = array_keys($result['mid']);
//$result['count'] = count($result['mid']);
if(empty($midflag)) unset($result['mid']);
$result['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$result['memory'] = (memory_get_usage(true) - self::$startMemory)/1024/1024 . ' M';
return $result;
}
/**
* TODO:跨時間段 統(tǒng)計參加$op用戶數(shù)據(jù)
* @param string $date
* @param int $appid
* @param string $op
* @param number $day
* @param bool $midflag :默認(rèn)為false 則 mid返回空數(shù)組;如設(shè)為true,則mid數(shù)組不為空
* @return array 形如("20140326":{"mid":[],"count":4571,"diffTime":0.0806441307068,"memory":"3.75 M"},
* "20140325":{"mid":[],"count":2181,"diffTime":0.0397667884827,"memory":"3.75 M"})
*/
public function getReturnUser($date , $appid , $op , $day=1 , $midflag=false)
{
$date_i=0;
for ($i =0; $i<$day ; $i++){
$date_i = date('Ymd' , strtotime($date)-$i*86400);
$result[$date_i] = self::countFileMID($date_i , $appid , $op , $midflag);
}
unset($date , $date_i , $appid , $op , $day);
return $result;
}
}
?>
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。