Lib_File2.php
class Lib_File2
{
//文件目录
private $root = '/data/wwwroot/kkpromo/data/';
//文件后缀
private $suffix = '.log';
//文件句柄
private $handle=null;
//一次读取文件的最大记录数
private $limit=40000;
//每行读取的字节长度
private $length=1024;
//开始时间
private $startTime=0;
//内存使用基准点
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!');
}
}
/**
* 功能:解析数据
* 格式: 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) $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 默认从第一行开始
* @param number $length 默认到$this->limit 读取的行数
* @param array $condition:array('mid'=>arrray() , 'ip'=>array() , ...) 过滤条件
* @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为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) {
$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:命令行下获取文件总记录数*
* @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)); // 对命令行参数进行安全转义
$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:统计{$data}.{$op}.log记录数
* @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:统计用户数
* @param string $date
* @param int $appid
* @param string $op
* @param bool $midflag :默认为false 则 mid返回空数组;如设为true,则mid数组不为空
* * @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为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 {
$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:跨时间段 统计参加$op用户数据
* @param string $date
* @param int $appid
* @param string $op
* @param number $day
* @param bool $midflag :默认为false 则 mid返回空数组;如设为true,则mid数组不为空
* @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 $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
class Lib_File1
{
//文件目录
private $root = '/data/wwwroot/kkpromo/data/';
//文件后缀
private $suffix = '.log';
//文件句柄
private $hander=null;
//一次读取文件的最大记录数
private $limit=40000;
//每行读取的字节长度
private $length=1024;
//开始时间
private $startTime=0;
//内存使用基准点
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!');
}
}
/**
* 功能:解析数据
* 格式: 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) $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 默认从第一行开始
* @param number $endNum 默认到$this->limit结束
* @param array $condition:array('mid'=>arrray() , 'ip'=>array() , ...) 过滤条件
* @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为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 {
$tmp = fgets($hander , $this->length);
if(empty($tmp)) continue;
if($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:命令行下获取文件总记录数*
* @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)); // 对命令行参数进行安全转义
$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:统计{$data}.{$op}.log记录数
* @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:统计用户数
* @param string $date
* @param int $appid
* @param string $op
* @param bool $midflag :默认为false 则 mid返回空数组;如设为true,则mid数组不为空
* * @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为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 {
$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:跨时间段 统计参加$op用户数据
* @param string $date
* @param int $appid
* @param string $op
* @param number $day
* @param bool $midflag :默认为false 则 mid返回空数组;如设为true,则mid数组不为空
* @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 $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;
}
}
?>

PHP在現代編程中仍然是一個強大且廣泛使用的工具,尤其在web開發領域。 1)PHP易用且與數據庫集成無縫,是許多開發者的首選。 2)它支持動態內容生成和麵向對象編程,適合快速創建和維護網站。 3)PHP的性能可以通過緩存和優化數據庫查詢來提升,其廣泛的社區和豐富生態系統使其在當今技術棧中仍具重要地位。

在PHP中,弱引用是通過WeakReference類實現的,不會阻止垃圾回收器回收對象。弱引用適用於緩存系統和事件監聽器等場景,需注意其不能保證對象存活,且垃圾回收可能延遲。

\_\_invoke方法允許對象像函數一樣被調用。 1.定義\_\_invoke方法使對象可被調用。 2.使用$obj(...)語法時,PHP會執行\_\_invoke方法。 3.適用於日誌記錄和計算器等場景,提高代碼靈活性和可讀性。

Fibers在PHP8.1中引入,提升了並發處理能力。 1)Fibers是一種輕量級的並發模型,類似於協程。 2)它們允許開發者手動控制任務的執行流,適合處理I/O密集型任務。 3)使用Fibers可以編寫更高效、響應性更強的代碼。

PHP社區提供了豐富的資源和支持,幫助開發者成長。 1)資源包括官方文檔、教程、博客和開源項目如Laravel和Symfony。 2)支持可以通過StackOverflow、Reddit和Slack頻道獲得。 3)開發動態可以通過關注RFC了解。 4)融入社區可以通過積極參與、貢獻代碼和學習分享來實現。

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),