• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php读取大文件示例分享(文件操作类)_php实例

php 搞代码 4年前 (2022-01-25) 16次浏览 已收录 0个评论

Lib_File2.php

<?php <BR> class Lib_File2<BR> {<BR>  //文件目录<BR>  private $root = '/data/wwwroot/kkpromo/data/';<br><br>  //文件后缀<BR>  private $suffix = '.log';<br><br>  //文件句柄<BR>  private $handle=null;<br><br>  //一次读取文件的最大记录数<BR>  private $limit=40000;<br><br>  //每行读取的字节长度<BR>  private $length=1024;<br><br>  //开始时间 <BR>  private  $startTime=0;<br><br>  //内存使用基准点<BR>  private static $startMemory=0;<br><br>  //<BR>  private $conn=null;<br><br>  //<BR>  private static  $init=null;<br><br>  public static function instance()<BR>  {<BR>   self::$startMemory = memory_get_usage(true);<br><br>   if(self::$init && is_object(self::$init))<BR>   {<BR>    return self::$init;<BR>   }<br><br>   self::$init = new self();<br><br>   return self::$init;<BR>  }<br><br>  private function __construct(){}<br><br>  public  function setRoot($root)   <BR>  {<BR>   if(!is_dir($root)) die($root.' ROOT DOES NOT EXIST');<BR>   $this->root = $root;<BR>  }<br><br>  public function setSuffix($suffix)<BR>  {<BR>   $this->suffix = $suffix;<BR>  }<br><br>  public function setLimit($limit)<BR>  {<BR>   if(!is_numeric($limit)) die($limit.' SHOULD BE NUMBERIC');<BR>   if(intval($limit) > 1000000) die($limit.' SHOULD BE LOWER THAN 1000000');<BR>   $this->limit = intval($limit);<BR>  }<br><br>  public function _getFile($date , $appid , $op)<BR>  {<BR>   $filename = rtrim($this->root , '/').DIRECTORY_SEPARATOR.$date.DIRECTORY_SEPARATOR.$appid.'.'.$op.$this->suffix;<BR>   if(!file_exists($filename))<BR>   {<BR>    die($filename.' FILE DOES NOT EXISTS!');<BR>   }<br><br>   if(!is_file($filename))<BR>   {<BR>    die($filename.' FILE DOES NOT EXIST!');<BR>   }<br><br>   if(!is_readable($filename))<BR>   {<BR>    die($filename.'  FILE ACCESS DENY!');<BR>   }<br><br>   return $filename;<BR>  }<br><br>  <BR>  public function closeFile($date=null , $appid=null , $op=null)<BR>  {<BR>   if($op && $date && $appid)<BR>   {<BR>    if(is_object($this->handle[$date.'_'.$appid.'_'.$op]) || $this->conn[$date.'_'.$appid.'_'.$op])<BR>    {<BR>     unset($this->handle[$date.'_'.$appid.'_'.$op]);<BR>     $this->handle[$date.'_'.$appid.'_'.$op]=null;<BR>    }<br><br>    $this->conn[$date.'_'.$appid.'_'.$op]=null;<BR>    $this->handle[$date.'_'.$appid.'_'.$op]=null;<BR>    unset($this->handle[$date.'_'.$appid.'_'.$op]);<BR>   }<BR>   else {<BR>    if(is_array($this->handle) && $this->handle)<BR>    {<BR>     foreach ($this->handle as $key=>$val){<BR>      unset($this->handle[$key]);<BR>      $this->conn[$key]=null;<BR>      $this->handle[$key]=null;<BR>     }<BR>    }<BR>   } <br><br>   return true;<BR>  }<br><br>  <BR>  private function _openFile($date , $appid , $op)<BR>  {   <BR>   $this->startTime = microtime(true);<BR>   if(isset($this->conn[$date.'_'.$appid.'_'.$op])  && $this->conn[$date.'_'.$appid.'_'.$op])<BR>   {<BR>    return $this->handle[$date.'_'.$appid.'_'.$op];<BR>   }<br><br>   $filename = self::_getFile($date , $appid , $op);<BR>   if(($this->handle[$date.'_'.$appid.'_'.$op] = new SplFileObject($filename , 'r'))!=null)<BR>   {<BR>    $this->conn[$date.'_'.$appid.'_'.$op] = true;<BR>    return $this->handle[$date.'_'.$appid.'_'.$op];<BR>   }<BR>   else {<BR>    die('FILE OPEN FAILED!');<BR>   }<BR>  }</P><P>  <BR>  /**<BR>   * 功能:解析数据<BR>   * 格式:  array('timestamp','mid','data');<BR>   * @param string $data<BR>   * @return boolean|array<BR>   */<BR>  private  function _parseData($data , $jsonFlag=true)<BR>  {<BR>   if(empty($data) || !is_string($data)) return false;<BR>   $result = array(<BR>     'timestamp'=>0,<BR>     'mid'=>0,<BR>     'data'=>array(),<BR>   );<br><br>   $data = explode('|', $data);<BR>   if(count($data) < 3 || !is_array($data)) return false;<BR>   $result['timestamp'] = $data[0];<BR>   $result['mid'] = $data[1];<BR>   if($jsonFlag)<BR>   {<BR>    $result['data'] = @json_decode($data[2] , true);<BR>    unset($result['mid']);<BR>   }<BR>   if(empty($result['timestamp']) || empty($result['mid'])) return false;<br><br>   unset($data);<BR>   return $result;<BR>  }<br><br>  <BR>  /**<BR>   * TODO:读取单一文件<BR>   * @param string $date: 如(20140327)<BR>   * @param int  $appid: 如(1000,9000)<BR>   * @param string $op:如(show,login , index)<BR>   * @param number $startNum 默认从第一行开始<BR>   * @param number $length 默认到$this->limit 读取的行数<BR>   * @param array $condition:array('mid'=>arrray() , 'ip'=>array() , ...)  过滤条件<BR>   * @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为false,则去掉false字段<BR>   * @return array(count , diffTime , memory , data)<BR>   */<BR>  public  function readFile($date , $appid , $op , $startNum=0 , $length=0 , $jsonFlag=false ,  $condition=array())<BR>  {<BR>   $data['data'] = "";<BR>   $data['count'] = 0;<BR>   $index = $startNum;<BR>   $startNum = empty($startNum) ? 0 : $startNum;<BR>   $length = empty($length) ? $this->limit : $length;<br><br>   $handle = self::_openFile($date , $appid , $op );<BR>   $line_number=0;<br><br>   if($handle)<BR>   { <BR>    $handle->seek($startNum);<BR>    $handle->setMaxLineLen($this->length);<BR>    while (intval($line_number) - intval($startNum) < intval($length)-1)<BR>    {<BR>     $tmp = $handle->current();<BR>     if(empty($tmp)) continue;     <BR>     $tmp = self::_parseData($tmp , $jsonFlag);<BR>     $line_number = $handle->key();<BR>     !$jsonFlag && $condition= array();        <BR>     if(isset($condition) && $condition)<BR>     {<BR>      $key = array_keys($condition);<BR>      if(in_array($tmp['data'][$key[0]], $condition[$key[0]]))<BR>      {<BR>       $data['count']++;<BR>       $data['data'][$line_number] = $tmp;<BR>      }<BR>     }<BR>     else<BR>     {<BR>      $data['data'][$line_number] = $tmp;<BR>      $data['count']++;<BR>     }<br><br>     if(intval($line_number) - intval($startNum) >= intval($length)-1) break;<BR>     unset($tmp);<BR>     $handle->next();<BR>    }<BR>    unset($tmp , $length , $line_number , $condition);<BR>   }<br><br>   $data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);<BR>   $data['memory'] = doubleval((doubleval(memory_get_usage(true)) -  doubleval(self::$startMemory))/1024/1024) . ' M';<br><br>   return $data;<BR>  }<BR>  </P><P>  /**<BR>   * TODO:命令行下获取文件总记录数*<BR>   * @param string $date<BR>   * @param int $appid<BR>   * @param string $op<BR>   * @return array<BR>   */<BR>  public  function total_lineFile($date, $appid, $op)<BR>  {<BR>   $this->_openFile($date, $appid, $op);<BR>   $file = escapeshellarg($this->_getFile($date, $appid, $op)); // 对命令行参数进行安全转义<BR>   $line = `wc -l  $file`;<BR>   if(preg_match("/(\d{1,})/", $line , $ret)){<BR>    $data['count']=$ret[1];<BR>   }else{<BR>    $data['count']=0;<BR>   }<BR>   $data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);<BR>   $data['memory'] = doubleval((doubleval(memory_get_usage(true)) -  doubleval(self::$startMemory))/1024/1024) . ' M';<BR>   return $data;<BR>  }</P><P>  <BR>  /**<BR>   * TODO:统计{$data}.{$op}.log记录数<BR>   * @param string $date<BR>   * @param int $appid<BR>   * @param string $op<BR>   * @param array $condition<BR>   * @return array<BR>   */<BR>  public function countFile($date , $appid , $op ,$condition=array())<BR>  {<BR>   $data['count'] = 0;<BR>   $handle = self::_openFile($date , $appid , $op );  <BR>   if($handle)<BR>   {<BR>    $handle->setMaxLineLen($this->length);<BR>    while (!$handle->eof())<BR>    {<BR>     $tmp = $handle->current();<BR>     if(empty($tmp)) continue;<BR>     $tmp = self::_parseData($tmp);<BR>     if($condition && is_array($condition) )<BR>     {      <BR>      $key = array_keys($condition);<BR>      if(isset($tmp['data'][$key[0]]) && $tmp['data'][$key[0]] && in_array($tmp['data'][$key[0]], $condition[$key[0]]))<BR>      {<BR>       $data['count']++;<BR>      }<BR>     }<BR>     else<BR>     {<BR>      $data['count']++;      <BR>     }<br><br>     unset($tmp);     <BR>     $handle->next();<BR>    }<BR>   }<BR>   unset($handle , $condition , $tmp , $key<p>+本文来源gao!%daima.com搞$代*!码9网(</p><strong>搞gaodaima代码</strong> , $val);<BR>   self::closeFile($date , $appid , $op );<br><br>   $data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);<BR>   $data['memory'] = doubleval((doubleval(memory_get_usage(true)) -  doubleval(self::$startMemory))/1024/1024) . ' M';<br><br>   return $data;<BR>  }  <br><br>  <br><br>  /**<BR>   * TODO:统计用户数<BR>   * @param string $date<BR>   * @param int $appid<BR>   * @param string $op<BR>   * @param bool $midflag :默认为false 则 mid返回空数组;如设为true,则mid数组不为空<BR>   * * @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为false,则去掉jsondata字段<BR>   * @param array $condition<BR>   * @return : array:形如({"mid":[],"count":2181,"diffTime":0.0397667884827,"memory":"3.75 M"})<BR>   */<BR>  public function countFileMID($date , $appid , $op  ,  $midflag=false , $jsonFlag=false,  $condition=array())<BR>  {<BR>   //$count = self::total_lineFile($date , $appid , $op );<BR>   $count = self::countFile($date , $appid , $op );<BR>   $index = ceil($count['count'] / $this->limit);<BR>   $result = array('mid'=>array() , 'count'=>0 , 'diffTime'=>0 , 'memory'=>0);</P><P>   for ($i=0 ; $i<$index ; $i++)<BR>   {<BR>    $startNum = $this->limit*$i;<BR>    $endNum = $this->limit;<BR>    $data = self::readFile($date , $appid , $op  ,  $startNum , $endNum , $jsonFlag);<BR>    var_dump($data);exit();</P><P>    if($data['data'] && is_array($data['data']))<BR>    {<BR>     foreach ($data['data'] as $arr)<BR>     {<BR>      if($condition && is_array($condition))<BR>      {<BR>       $key = array_keys($condition);<BR>       if(isset($arr['data'][$key[0]])  && (in_array($arr['data'][$key[0]] , $condition[$key]) || empty($condition[$key[0]])))<BR>       {<BR>        $result['mid'][$arr["mid"]] =1;<BR>        $result['count']++;<BR>       }<BR>      }<BR>      else<BR>      {<BR>        $result['mid'][$arr["mid"]] =1; <BR>        $result['count']++;<BR>      }<BR>      unset($data);<BR>     }<BR>    }<BR>   }<BR>   unset($index , $count , $condition , $data  , $arr);<BR>   self::closeFile($date , $appid , $op);<br><br>   $result['mid'] = array_keys($result['mid']);  <BR>   if(empty($midflag)) unset($result['mid']); <br><br>   $result['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);<BR>   $result['memory'] = (memory_get_usage(true) - self::$startMemory)/1024/1024 . ' M';<BR>   return $result;<BR>  }<br><br>  <BR>  /**<BR>   * TODO:跨时间段 统计参加$op用户数据<BR>   * @param string $date<BR>   * @param int $appid<BR>   * @param string $op<BR>   * @param number $day<BR>   * @param bool  $midflag :默认为false 则 mid返回空数组;如设为true,则mid数组不为空<BR>   * @return array 形如("20140326":{"mid":[],"count":4571,"diffTime":0.0806441307068,"memory":"3.75 M"},<BR>  *          "20140325":{"mid":[],"count":2181,"diffTime":0.0397667884827,"memory":"3.75 M"})<BR>   */<BR>  public function getReturnUser($date , $appid , $op , $day=1 , $midflag=false)<BR>  {<BR>   $date_i=0;<BR>   for ($i =0; $i<$day ; $i++){<BR>    $date_i = date('Ymd' , strtotime($date)-$i*86400);<BR>    $result[$date_i] = self::countFileMID($date_i , $appid , $op , $midflag); <BR>   }<BR>   unset($date , $date_i , $appid , $op  , $day);<br><br>   return $result;<BR>  }<BR> }<BR>?><BR>

Lib_File1.php

<?php <BR> class Lib_File1<BR> {<BR>  //文件目录<BR>  private $root = '/data/wwwroot/kkpromo/data/';<br><br>  //文件后缀<BR>  private $suffix = '.log';<br><br>  //文件句柄<BR>  private $hander=null;<br><br>  //一次读取文件的最大记录数<BR>  private $limit=40000;<br><br>  //每行读取的字节长度<BR>  private $length=1024;<br><br>  //开始时间 <BR>  private  $startTime=0;<br><br>  //内存使用基准点<BR>  private static $startMemory=0;<br><br>  //<BR>  private $conn=null;<br><br>  //<BR>  private static  $init=null;<br><br>  public static function instance()<BR>  {<BR>   self::$startMemory = memory_get_usage(true);<br><br>   if(self::$init && is_object(self::$init))<BR>   {<BR>    return self::$init;<BR>   }<br><br>   self::$init = new self();<br><br>   return self::$init;<BR>  }<br><br>  private function __construct(){}<br><br>  public  function setRoot($root)   <BR>  {<BR>   if(!is_dir($root)) die($root.' ROOT DOES NOT EXIST');<BR>   $this->root = $root;<BR>  }<br><br>  public function setSuffix($suffix)<BR>  {<BR>   $this->suffix = $suffix;<BR>  }<br><br>  public function setLimit($limit)<BR>  {<BR>   if(!is_numeric($limit)) die($limit.' SHOULD BE NUMBERIC');<BR>   if(intval($limit) > 1000000) die($limit.' SHOULD BE LOWER THAN 1000000');<BR>   $this->limit = intval($limit);<BR>  }<br><br>  private function _getFile($date , $appid , $op)<BR>  {<BR>   $filename = rtrim($this->root , '/').DIRECTORY_SEPARATOR.$date.DIRECTORY_SEPARATOR.$appid.'.'.$op.$this->suffix;<BR>   if(!file_exists($filename))<BR>   {<BR>    die($filename.' FILE DOES NOT EXISTS!');<BR>   }<br><br>   if(!is_file($filename))<BR>   {<BR>    die($filename.' FILE DOES NOT EXIST!');<BR>   }<br><br>   if(!is_readable($filename))<BR>   {<BR>    die($filename.'  FILE ACCESS DENY!');<BR>   }<br><br>   return $filename;<BR>  }<br><br>  <BR>  public function closeFile($date=null , $appid=null , $op=null)<BR>  {<BR>   if($op && $date && $appid)<BR>   {<BR>    if(is_object($this->hander[$date.'_'.$appid.'_'.$op]) || $this->conn[$date.'_'.$appid.'_'.$op])<BR>    {<BR>     fclose($this->hander[$date.'_'.$appid.'_'.$op]);<BR>    }<br><br>    $this->conn[$date.'_'.$appid.'_'.$op]=null;<BR>    $this->hander[$date.'_'.$appid.'_'.$op]=null;<BR>   }<BR>   else {<BR>    if(is_array($this->hander) && $this->hander)<BR>    {<BR>     foreach ($this->hander as $key=>$val){<BR>      fclose($this->hander[$key]);<BR>      $this->conn[$key]=null;<BR>      $this->hander[$key]=null;<BR>     }<BR>    }<BR>   } <br><br>   return true;<BR>  }<br><br>  <BR>  private function _openFile($date , $appid , $op)<BR>  {   <BR>   $this->startTime = microtime(true);<BR>   if(isset($this->conn[$date.'_'.$appid.'_'.$op])  && $this->conn[$date.'_'.$appid.'_'.$op])<BR>   {<BR>    return $this->hander[$date.'_'.$appid.'_'.$op];<BR>   }<br><br>   $filename = self::_getFile($date , $appid , $op);<BR>   if(($this->hander[$date.'_'.$appid.'_'.$op] = fopen($filename, 'r'))!=null)<BR>   {<BR>    $this->conn[$date.'_'.$appid.'_'.$op] = true;<BR>    return $this->hander[$date.'_'.$appid.'_'.$op];<BR>   }<BR>   else {<BR>    die('FILE OPEN FAILED!');<BR>   }<BR>  }</P><P>  <BR>  /**<BR>   * 功能:解析数据<BR>   * 格式:  array('timestamp','mid','data');<BR>   * @param string $data<BR>   * @return boolean|array<BR>   */<BR>  private  function _parseData($data)<BR>  {<BR>   if(empty($data) || !is_string($data)) return false;<BR>   $result = array(<BR>     'timestamp'=>0,<BR>     'mid'=>0,<BR>     'data'=>array(),<BR>   );<br><br>   $data = explode('|', $data);<BR>   if(count($data) < 3 || !is_array($data)) return false;<BR>   $result['timestamp'] = $data[0];<BR>   $result['mid'] = $data[1];<BR>   $result['data'] = @json_decode($data[2] , true);<BR>   if(empty($result['timestamp']) || empty($result['mid'])) return false;<br><br>   unset($data);<BR>   return $result;<BR>  }<br><br>  <BR>  /**<BR>   * TODO:读取单一文件<BR>   * @param string $date: 如(20140327)<BR>   * @param int  $appid: 如(1000,9000)<BR>   * @param string $op:如(show,login , index)<BR>   * @param number $startNum 默认从第一行开始<BR>   * @param number $endNum 默认到$this->limit结束<BR>   * @param array $condition:array('mid'=>arrray() , 'ip'=>array() , ...)  过滤条件<BR>   * @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为false,则去掉false字段<BR>   * @return array(count , diffTime , memory , data)<BR>   */<BR>  public  function readFile($date , $appid , $op ,$startNum=0 , $endNum=0 , $jsonFlag=false ,  $condition=array())<BR>  {<BR>   $data['data'] = "";<BR>   $data['count'] = 0;<BR>   $index = $startNum;<BR>   $startNum = empty($startNum) ? 0 : $startNum;<BR>   $endNum = empty($endNum) ? $this->limit : $endNum;<br><br>   $hander = self::_openFile($date , $appid , $op );<BR>   $tmpindex=0;<br><br>   if($hander)<BR>   {<BR>    //!feof($hander)<BR>    while ($tmpindex < $endNum)<BR>    {<BR>     $tmp = fgets($hander , $this->length);<BR>     if(empty($tmp)) continue;<BR>     if($tmpindex =$startNum)<BR>     {<BR>      $tmp = self::_parseData($tmp);    <BR>      if(empty($tmp)) continue;<BR>      //去掉jsondata<BR>       if(!$jsonFlag) { unset($tmp[2]);  $condition= array();  }<BR>      //条件过滤<BR>      if($condition && is_array($condition) )<BR>      {<BR>       foreach ($condition as $key=>$val){<BR>        if(in_array($tmp['data'][$key], $condition[$key]))<BR>         unset($key , $val); <BR>         $data['count']++;<BR>         $data['data'][$index] = $tmp;<BR>         $index++;<BR>        }<BR>      }<BR>      else{    <BR>       $data['data'][$index] = $tmp;<BR>       $index++;<BR>       $data['count']++;<BR>      }<BR>     }<BR>     if($tmpindex >= $endNum) break;<BR>     $tmpindex++;<BR>     unset($tmp);<BR>    }<BR>    fseek($hander ,  SEEK_END);<BR>   }<br><br>   $data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);<BR>   $data['memory'] = doubleval((doubleval(memory_get_usage(true)) -  doubleval(self::$startMemory))/1024/1024) . ' M';<br><br>   return $data;<BR>  }<BR>  </P><P>  /**<BR>   * TODO:命令行下获取文件总记录数*<BR>   * @param string $date<BR>   * @param int $appid<BR>   * @param string $op<BR>   * @return array<BR>   */<BR>  public  function total_lineFile($date, $appid, $op)<BR>  {<BR>   $this->_openFile($date, $appid, $op);<BR>   $file = escapeshellarg($this->_getFile($date, $appid, $op)); // 对命令行参数进行安全转义<BR>   $line = `wc -l  $file`;<BR>   if(preg_match("/(\d{1,})/", $line , $ret)){<BR>    $data['count']=$ret[1];<BR>   }else{<BR>    $data['count']=0;<BR>   }<BR>   $data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);<BR>   $data['memory'] = doubleval((doubleval(memory_get_usage(true)) -  doubleval(self::$startMemory))/1024/1024) . ' M';<BR>   return $data;<BR>  }</P><P>  <BR>  /**<BR>   * TODO:统计{$data}.{$op}.log记录数<BR>   * @param string $date<BR>   * @param int $appid<BR>   * @param string $op<BR>   * @param array $condition<BR>   * @return array<BR>   */<BR>  public function countFile($date , $appid , $op ,$condition=array())<BR>  {<BR>   $data['count'] = 0;<BR>   $hander = self::_openFile($date , $appid , $op );  <BR>   if($hander)<BR>   {<BR>    while (!feof($hander))<BR>    {<BR>     $tmp = fgets($hander , $this->length);<BR>     $tmp = self::_parseData($tmp);<BR>     if(empty($tmp)) continue;<BR>     if($condition && is_array($condition) )<BR>     {<BR>      foreach ($condition as $key=>$val){<BR>       if(isset($tmp['data'][$key]) && $tmp['data'][$key] && in_array($tmp['data'][$key], $condition[$key])){<BR>        unset($key , $val); <BR>        $data['count']++;<BR>       } <BR>      }<BR>     }<BR>     else<BR>      $data['count']++;      <BR>     unset($tmp);<BR>    }<BR>    fseek($hander , SEEK_END);<BR>   }<BR>   $data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);<BR>   $data['memory'] = doubleval((doubleval(memory_get_usage(true)) -  doubleval(self::$startMemory))/1024/1024) . ' M';<br><br>   return $data;<BR>  }  <br><br>  <br><br>  /**<BR>   * TODO:统计用户数<BR>   * @param string $date<BR>   * @param int $appid<BR>   * @param string $op<BR>   * @param bool $midflag :默认为false 则 mid返回空数组;如设为true,则mid数组不为空<BR>   * * @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为false,则去掉jsondata字段<BR>   * @param array $condition<BR>   * @return : array:形如({"mid":[],"count":2181,"diffTime":0.0397667884827,"memory":"3.75 M"})<BR>   */<BR>  public function countFileMID($date , $appid , $op  ,  $midflag=false , $jsonFlag=false,  $condition=array())<BR>  {<BR>   $count = self::total_lineFile($date , $appid , $op );<BR>   $index = ceil($count['count'] / $this->limit);<BR>   $result = array('mid'=>array() , 'count'=>0 , 'diffTime'=>0 , 'memory'=>0);</P><P>   for ($i=0 ; $i<$index ; $i++)<BR>   {<BR>    $startNum = $this->limit*$i;<BR>    $endNum = $this->limit*($i+1);<BR>    $data = self::readFile($date , $appid , $op  ,  $startNum , $endNum , $jsonFlag);<BR>    if($data['data'] && is_array($data['data']))<BR>    {<BR>     foreach ($data['data'] as $arr)<BR>     {<BR>      if($condition && is_array($condition)){<BR>       foreach ($condition as $key=>$val){ <BR>        if(isset($arr['data'][$key])  && (in_array($arr['data'][$key] , $condition[$key]) || empty($condition[$key]))){<BR>         if(!isset($result['mid'][$arr['mid']]))  { $result['mid'][$arr["mid"]] =1; $result['count']++; }<BR>        }<BR>       }<BR>      }<BR>      else {<BR>       if(!isset($result['mid'][$arr['mid']]))  { $result['mid'][$arr["mid"]] =1; $result['count']++;   }<BR>      }<BR>     }<BR>    }<BR>    unset($data['data'] , $data); <BR>   }<BR>   unset($index , $count , $condition , $data  , $arr);<BR>   self::closeFile($date , $appid , $op);<br><br>    $result['mid'] = array_keys($result['mid']);<BR>    //$result['count'] = count($result['mid']);   <BR>    if(empty($midflag)) unset($result['mid']); <br><br>   $result['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);<BR>   $result['memory'] = (memory_get_usage(true) - self::$startMemory)/1024/1024 . ' M';<BR>   return $result;<BR>  }<br><br>  <BR>  /**<BR>   * TODO:跨时间段 统计参加$op用户数据<BR>   * @param string $date<BR>   * @param int $appid<BR>   * @param string $op<BR>   * @param number $day<BR>   * @param bool  $midflag :默认为false 则 mid返回空数组;如设为true,则mid数组不为空<BR>   * @return array 形如("20140326":{"mid":[],"count":4571,"diffTime":0.0806441307068,"memory":"3.75 M"},<BR>  *          "20140325":{"mid":[],"count":2181,"diffTime":0.0397667884827,"memory":"3.75 M"})<BR>   */<BR>  public function getReturnUser($date , $appid , $op , $day=1 , $midflag=false)<BR>  {<BR>   $date_i=0;<BR>   for ($i =0; $i<$day ; $i++){<BR>    $date_i = date('Ymd' , strtotime($date)-$i*86400);<BR>    $result[$date_i] = self::countFileMID($date_i , $appid , $op , $midflag); <BR>   }<BR>   unset($date , $date_i , $appid , $op  , $day);<br><br>   return $result;<BR>  }<BR> }<BR>?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php读取大文件示例分享(文件操作类)_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址