对于大文件只获取部分数据很有用

1.使用ftell函数可以获取当前指针的字节位置
2.使用fseek函数可以直接定位到指定的位置
3.读取指定字节的数据就可以部分获取文件内容了

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

1.log的内容

2.获取部分结果

[PHP] 使用ftell和fseek函数直接定位文件位置获取部分数据的更多相关文章

  1. php使用file函数、fseek函数读取大文件效率分析

    php读取大文件可以使用file函数和fseek函数,但是二者之间效率可能存在差异,本文章向大家介绍php file函数与fseek函数实现大文件读取效率对比分析,需要的朋友可以参考一下. 1. 直接 ...

  2. Eclipse 进入代码定位文件位置

  3. shell 字符串中定位字符位置 获取字符位置

    linux shell 字符串操作(长度,查找,替换)详解 该博文中描述的如下两个字符串操作, ${string:position} #在$string中, 从位置$position开始提取子串 ${ ...

  4. 文件操作:fseek函数和ftell函数

    1.fseek函数: int fseek(FILE * _File, long _Offset, int _Origin); 函数设置文件指针stream的位置.如果执行成功,stream将指向以fr ...

  5. 如何通过ftell和fseek来获取文件大小

    #include <stdio.h> int main () { FILE * pFile=NULL; long size; pFile = fopen ("myfile.txt ...

  6. PHP fseek() 函数

    定义和用法 fseek() 函数在打开的文件中定位. 该函数把文件指针从当前位置向前或向后移动到新的位置,新位置从文件头开始以字节数度量. 如果成功该函数返回 0,如果失败则返回 -1.请注意,移动到 ...

  7. C中的fseek函数使用

    函数名:fseek函数 头文件:#include<stdio.h> 功能:把与fp有关的文件位置指针放到一个指定位置. 格式:  int fseek(FILE *stream, long ...

  8. PHP移动文件指针ftell()、fseek()、rewind()总结

    在对文件进行读写过程中,有时需要在文件中跳转.同不同位置读取,以及将数据写入到不同的位置.例如,使用文件模拟数据库保存数据,就需要移动文件指针.指针的位置是以从文件头开始的字节数度量的,默认以不同模式 ...

  9. strtok()函数、fseek()函数、fwrite()函数、fread()函数的使用

    在电子词典这个项目过程中遇到了几个主要的问题,第一个是怎么解决把翻译分开这个.第二个事情就是怎么把结构体写到文件中.这两个问题,一个是关于字符串的操作一个是关于文件的操作. strtok函数 char ...

随机推荐

  1. 学习CNN系列一:原理篇

    CNN的发展历程: 1962年,卷积神经网络的研究起源于Hubel和Wiesel研究毛脑皮层的发现局部互连网络可以有效降低反馈神经网络的复杂性. 1980年,CNN的第一个实现网络:Fukushima ...

  2. 浅谈AMD与CMD

    AMD 是 RequireJS 在推广过程中对模块定义的规范化产出. CMD 是 SeaJS 在推广过程中对模块定义的规范化产出. 这些规范的目的都是为了 JavaScript 的模块化开发,特别是在 ...

  3. jmeter判断请求响应时间,请求下加‘断言持续时间’,添加监听器断言结果;统计超时

  4. 如何在类中根据枚举值,获取枚举的message的工具类

    枚举类为: public enum OrderStatusEnum implements CondeEnum{ NEW(0, "新订单"), FINISHED(1, "完 ...

  5. mysql数据库的批量数据导入与导出,性能提升。

    少量数据批量导入:1. 先从数据库把唯一键的值查询出来,放在列表2. 将导入的数据遍历取出,看是否存在列表中,若不在,说明数据库没有.3. 定义两个空列表,一个做为插入数据,一个做为更新数据4. 步骤 ...

  6. 【BZOJ3534】[SDOI2014] 重建(矩阵树定理)

    点此看题面 大致题意: 给你一张图,每条边有一定存在概率.求存在的图刚好为一棵树的概率. 矩阵树定理是什么 如果您不会矩阵树定理,可以看看蒟蒻的这篇博客:初学矩阵树定理. 矩阵树定理的应用 此题中,直 ...

  7. 送书『构建Apache Kafka流数据应用』和『小灰的算法之旅』和『Java并发编程的艺术』

    读书好处 1.可以使我们增长见识. 2.可提高我们的阅读能力和写作水平. 3.可以使我们变的有修养. 4.可以使我们找到好工作. 5.可以使我们在竞争激烈的社会立于不败之地. 6.最大的好处是可以让你 ...

  8. 未初始化内存检测(MSan)

    https://github.com/google/sanitizers/wiki https://github.com/google/sanitizers/wiki/MemorySanitizer ...

  9. tensorflow学习笔记——使用TensorFlow操作MNIST数据(1)

    续集请点击我:tensorflow学习笔记——使用TensorFlow操作MNIST数据(2) 本节开始学习使用tensorflow教程,当然从最简单的MNIST开始.这怎么说呢,就好比编程入门有He ...

  10. F#周报2019年第22期

    新闻 2019年实用F#挑战结果 FSharp.Formatting正在确定维护者 实用F#挑战的赢家们 使用F#在分布式系统中进行故障检测与共识 F#里的Cloudflare Worker Juni ...