/**
* 文件读写类
* 读取时,支持跳过N个/行字符然后再读取M个/行字符
* 支持每次读取时使用回调函数
*
* 示例:
* $file = new File('a.txt', 'r');
*
* $data = $file->limit(0, 10)->getLine(function($line){
* return $line . ' <br/>';
* });
* print_r($data);
* $file->close();
*
*/ class File { protected $fp = null;
protected $file = null;
protected $limit = [0, 0]; public function __construct($file, $mode = 'r'){ $this->file = $file;
$this->fp = fopen($file, $mode); } public function limit($start, $line = 0){ if(func_num_args() == 1){
$line = $start; $start = 0;
}
$this->limit = [$start, $line];
return $this;
} /**
* 增加对文件函数的直接调用
* @param string $method
* @param mixed $args
* @return mixed
*/
public function __call($method, $args){ array_unshift($args, $this->fp);
return call_user_func_array($method, $args);
} /**
* 读取文件按行
* @param function $func
* @param $read 读取方式
* @param $args 参数列表
* @return mixed
*/
public function readLine($func = null, $read = null, $args = []){ $data = array();
$opt = array('char'=>'fgetc', 'line'=>'fgets', 'csv'=>'fgetcsv');
$read = (isset($opt[$read]))? $opt[$read] : 'fgets';
array_unshift($args, $this->fp); //跳过字符数或行数
if($this->limit[0] !== 0){
for($i = 0; $i < $this->limit[0]; $i++){
if(call_user_func_array($read, $args) === false){
return $data;
}
}
} if($this->limit[1] > 0){
//读取有效行数
for($i = 0; $i < $this->limit[1]; $i++){ if(($buffer = call_user_func_array($read, $args)) === false){
return $data;
}
if(is_callable($func)){
$buffer = $func($buffer);
if($buffer !== null) $data[] = $buffer;
}else{
$data[] = $buffer;
}
} }else{
//读取所有行数
while(($buffer = call_user_func_array($read, $args)) !== false){
if(is_callable($func)){
$buffer = $func($buffer);
if($buffer !== null) $data[] = $buffer;
}
}
}
$this->setHome();
return $data; } /**
* 读取文件按字符
* @param function $func
* @return mixed
*/
public function getChar($func = null){
return implode('', $this->readLine($func, 'char')); } /**
* 读取文件按行
* @param int $length
* @param function $func
* @return mixed
*/
public function getLine($func = null, $length = 1024){ if(is_numeric($func)){
$length = $func; $func = null;
}
return $this->readLine($func, 'line', [$length]);
} /**
* 读按行取csv文件
* @param unknown $func
* @param number $length
* @param string $delimiter
*/
public function getCsv($length = 0, $delimiter = ','){
return $this->readLine($func, 'csv', [$length, $delimiter]);
} /**
* 读取文件按字节
* @param number $length
* @return string
*/
public function read($length = 0){ if(empty($length)) $length = filesize($this->file);
return fread($this->fp, $length);
} /**
* 写入文件
* @param unknown $string
* @param unknown $length
* @return number
*/
public function write($string, $length = null){
return fwrite($this->fp, $string, $length);
} /**
* 写入csv文件
* @param unknown $data
* @param string $delimiter
* @return boolean
*/
public function putCsv($data, $delimiter = ','){ foreach($data as $row) {
fputcsv($fp, $row);
}
} /**
* 获取文件资源指针
* @return resource
*/
public function getfp(){
return $this->fp;
} /**
* 获取当前头文件指针位置
* @return number
*/
public function getSeek(){
return ftell($this->fp);
} /**
* 文件指针定位到开头
* @return boolean
*/
public function setHome(){
return rewind($this->fp);
} /**
* 文件指针定位到结尾
* @return number
*/
public function setEnd(){
return fseek($this->fp, 0, SEEK_END);
} /**
* 设置文件指针位置
* @param unknown $offset
* @param string $whence
* @return number
*/
public function setSeek($offset, $whence = SEEK_SET){
return fseek($this->fp, $offset, $whence);
} /**
* 关闭文件指针
* @return boolean
*/
public function close(){
return fclose($this->fp);
} public static function getSql($file, $isarray = false){ $content = preg_replace(['/[\r|\n]+\s?(((--\s+|#).*)|\/\*(.|\n)*\*\/)/', '/[\r|\n]+/'], '', file_get_contents($file));
if($isarray) $content = array_filter(explode(';', $content));
return $content;
} }

PHP文件处理类的更多相关文章

  1. php 文件日志类

    php文件日志类,按年月日组织目录结构. <?php class FileLog { private $_filepath; //文件路径 private $_filename; //日志文件名 ...

  2. C++文件流类与文件流对象

    文件流是以外存文件为输入输出对象的数据流.输出文件流是从内存流向外存文件的数据,输入文件流是从外存文件流向内存的数据.每一个文件流都有一个内存缓冲区与之对应. 请区分文件流与文件的概念,不用误以为文件 ...

  3. 使用Java的多线程和IO流写一个文件复制功能类

    创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...

  4. Code片段 : .properties属性文件操作工具类 & JSON工具类

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “贵专” — 泥瓦匠 一.java.util.Properties API & 案例 j ...

  5. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  6. 读取Config文件工具类 PropertiesConfig.java

    package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...

  7. C++学习47 文件的概念 文件流类与文件流对象 文件的打开与关闭

    迄今为止,我们讨论的输入输出是以系统指定的标准设备(输入设备为键盘,输出设备为显示器)为对象的.在实际应用中,常以磁盘文件作为对象.即从磁盘文件读取数据,将数据输出到磁盘文件.磁盘是计算机的外部存储器 ...

  8. C# - 文件操作类

    除了封装数据流的类 System.IO命名空间中还提供了可以操作文件和目录的类 Directory类 ns:System.IO Level:Object=>Directory 表示目录的类 用于 ...

  9. php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event

    php加了命名空间没引入初始化文件:类的命名空间要与文件夹名一致namespace Business\Event;缺少了Event

随机推荐

  1. Visual Studio Online Integrations-Testing

    原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs

  2. Ultra-QuickSort

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 44489   Accepted: 16176 ...

  3. MSSQL复习

    1.用户角色: 登录名就相当于一个用户 角色相当于把你的操作权限分组了 2.数据系统结构(略) 网络连接接口 关系引擎 存储引擎 内存 3.数据库的结构 数据库 架构 对象(在Sql server中将 ...

  4. Android相机、相册获取图片显示并保存到SD卡

    Android相机.相册获取图片显示并保存到SD卡 [复制链接]   电梯直达 楼主    发表于 2013-3-13 19:51:43 | 只看该作者 |只看大图  本帖最后由 happy小妖同学 ...

  5. 快速诊断Linux性能

    导读 当你为了解决一个性能问题登录到一台 Linux 服务器:在第一分钟你应该检查些什么? 通过运行下面十个命令,你就能在六十秒内粗略地了解系统正在运行的进程及资源使用情况.通过查看这些命令输出的错误 ...

  6. opencv中,c和c++版本区别体验

    参考:http://www.cnblogs.com/tornadomeet/archive/2012/04/29/2476277.html

  7. HLG2035广搜

    Diablo Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 42(21 users) Total Accepted: 23(20 us ...

  8. jquery博客收集的IE6中CSS常见BUG全集及解决方案

    今天的样式调的纠结,一会这边一会那么把jquery博客折腾的头大,浏览器兼容性.晚上闲着收集一些常见IE6中的BUG 3像素问题及解决办法 当使用float浮动容器后,在IE6下会产生3px的空隙,有 ...

  9. PHP 冒泡原理

    header('Content-Type: text/html; charset=utf-8'); // 简单冒泡算法 $a = array(5,43,3,2,1); function mp($a){ ...

  10. 19.python笔记之Rabbitmq

    RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(MQ)是一种应用程序 ...