1.使用ftell函数可以获取当前指针的字节位置
2.使用fseek函数可以直接定位到指定的位置
3.读取指定字节的数据就可以部分获取文件内容了
<?PHP class FileStream { private $fp = null; $mode = 'r'$context = $readonly = false$writeonly = $appendMode = ; public function __construct($file,$mode = 'r',1)">) { $mode = trim($mode); if (isset($mode[0])) { $this->mode = strtolower(); } if ($context) { $this->context = ; $this->fp = fopen($mode,false,1)">$this->context); } else { if (!fp) { throw new Exception('can not open ' . $file); } $this->mode == 'r'$this->readonly = true; } elseif ($this->mode == 'w'$this->writeonly = $this->mode[0] == 'a'$this->appendMode = ; } } function __destruct() { close(); } close() { fclose(fp); function read($size) { writeonly) { Exception('write only'Exception('stream already closed'); } $buf = fread($this->fp,1)">$buf === ) { Exception('read Failed'return $buf; } readLine() { ); } return fgets(fp); } readAll() { $buf = ''; while ($s = ); $s === ) { ); } if (!$s[0])) { break; } $buf .= $s; } function write($datareadonly) { Exception('read only'fwrite($data) === Exception('write Failed'); } } tell() { appendMode) { Exception('tell can not work on appendmode'$p = ftell(fp); $p === Exception('tell Failed'$pfunction seek($positionException('seek can not work on seekmode'fseek($position) !== 0Exception('seek Failed'); } } } $stream=new FileStream("1.log"); $start=0; $end=0; //获取开始和结束的字节位置 while($ln=$stream->readLine()){ if($ln=="3333333333333\r\n"){ $start=tell(); } $ln=="5555555555555\r\n"){ $end=tell(); } } var_dump($start,1)">$end); 直接定位到开始的字节位置 $stream->seek($start); 读取指定字节数的数据 $res=$stream->read($end - $res);
1.log的内容
2.获取部分结果