/**
* 文件读写类
* 读取时,支持跳过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. VirtualBox下安装rhel5.5 linux系统

    以前也用过VMware server和VMware workstation虚拟机,现在使用了一段时间VirtualBox,感觉它比较轻巧,很适合我,在Win7系统下用起来很方便.下面详细介绍下在Vir ...

  2. MySql数据类型详解

    可配合http://www.cnblogs.com/langtianya/archive/2013/03/10/2952442.html学习 MySql数据类型 1.整型(xxxint)   MySQ ...

  3. Centos上传下载小工具lrzsz

    http://www.centoscn.com/image-text/install/2013/0819/1374.html

  4. 一个PHP写的简单webservice服务端+客户端

    首先是服务端,服务端有一个主要的class组成:apiServer.php <?php /** * apiServer.php * * webservice主类 * * @filename ap ...

  5. iphone/ipad图标尺寸

    http://www.yixieshi.com/ucd/13759.html APP界面设计规范指导APP设计过程中的设计标准,根据统一的设计标准,使得整个APP在视觉上统一.提高用户对APP的产品认 ...

  6. Delphi与C语言类型转换对照

    When converting C function prototypes to Pascal equivalent declarations, it's important to substitut ...

  7. 分享一个强大的采集类,还可以模拟php多进程

    做采集的时候,可以使用file_get_contents()去获取网页源代码,但是使用file_get_contents采集,速度慢,而且超时时间,不好控制.如果采集的页面不存在,需要等待的时间很长. ...

  8. FastCgi与PHP-fpm之间是个什么样的关系

    刚开始对这个问题我也挺纠结的,看了<HTTP权威指南>后,感觉清晰了不少. 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. ...

  9. JQuery $(function(){})和$(document).ready(function(){})

    document.ready和onload的区别——JavaScript文档加载完成事件页面加载完成有两种事件一是ready,表示文档结构已经加载完成(不包含图片等非文字媒体文件)二是onload,指 ...

  10. 43. 动态规划求解n个骰子的点数和出现概率(或次数)[Print sum S probability of N dices]

    [题目] 把N个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入N,打印出S的所有可能的值出现的概率. [分析] 典型的动态规划题目. 设n个骰子的和为s出现的次数记为f(n,s),其中n=[1-N ...