<?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 , 'https://www.jb51.net/').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!');
}
}
php读取大文件示例分享(文件操作类)(4)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/2702c6927e7b1112b19e7da1da893478.html