php利用SPL实现针对大文件的读取
来源:互联网 发布:java 微商城开源框架 编辑:程序博客网 时间:2024/05/18 01:55
<?php
/*php 利用SPL实现针对大文件的读取*/header("Content-type: text/html; charset=utf-8");set_time_limit(0);class CsvReader { private $csv_file; private $spl_object = null; private $error; public function __construct($csv_file = '') { if($csv_file && file_exists($csv_file)) { $this->csv_file = $csv_file; } } public function set_csv_file($csv_file) { if(!$csv_file || !file_exists($csv_file)) { $this->error = 'File invalid'; return false; } $this->csv_file = $csv_file; $this->spl_object = null; } public function get_csv_file() { return $this->csv_file; } private function _file_valid($file = '') { $file = $file ? $file : $this->csv_file; if(!$file || !file_exists($file)) { return false; } if(!is_readable($file)) { return false; } return true; } private function _open_file() { if(!$this->_file_valid()) { $this->error = 'File invalid'; return false; } if($this->spl_object == null) { $this->spl_object = new SplFileObject($this->csv_file, 'rb'); } return true; } public function get_data($length = 0, $start = 0) { if(!$this->_open_file()) { return false; } $length = $length ? $length : $this->get_lines(); $start = $start - 1; $start = ($start < 0) ? 0 : $start; $data = array(); $this->spl_object->seek($start); while ($length-- && !$this->spl_object->eof()) { $data[] = $this->spl_object->fgetcsv(); $this->spl_object->next(); } return $data; } public function get_lines() { if(!$this->_open_file()) { return false; } $this->spl_object->seek(filesize($this->csv_file)); return $this->spl_object->key(); } public function get_error() { return $this->error; }}$csv_file = 'aaa.csv';$csvreader = new CsvReader($csv_file);$line_number = $csvreader->get_lines();$page = isset($_GET['p']) ? intval($_GET['p']) : 1;if($page == 1){ @unlink('./result.txt'); @unlink('./error_info_not_find.txt');}$num = 100;$total_page = ceil($line_number/$num);if($page > $total_page){ echo "处理结束";exit();}$current_num = ($page-1)*$num+1;$data = $csvreader->get_data($num,$current_num);foreach($data as $key=>$value){ $catId = $value[0]; $Id = $value[1]; $time = strtotime($value[5]); if($catId != "" && $Id !="" && $time !=""){ $url = 'http://fj.china.com.cn/'.date('Y-m/d',$time).'/content_'.$Id.'.htm'; file_put_contents('./result.txt',$url.PHP_EOL,FILE_APPEND); }else{ file_put_contents('./error_info_not_find.txt',"catId:$catId Id:$Id time:$time".PHP_EOL,FILE_APPEND); }}echo "总共:$total_page 页 当前$page 页 完成度 ".round($page/$total_page*100,2)."%";echo '<script language="javascript" type="text/javascript">window.location.href="./a.php?p='.++$page.'"</script>';?>
阅读全文
0 0
- php利用SPL实现针对大文件的读取
- [转]利用 PHP 的 SPL 快速实现 Observer 设计模式
- 利用PHP实现在线下载大文件
- PHP 读取大文件
- PHP读取大文件
- PHP读取大文件
- php关于大文件的读取
- 读取大excel文件的一种方式,利用PHPExcel读取
- PHP SPL的使用
- PHP SPL的使用
- php的spl
- Java读取大文件的高效率实现
- Java读取大文件的高效率实现
- Java读取大文件的高效率实现
- Java读取大文件的高效率实现
- Java读取大文件的高效率实现
- php中读取大文件实现方法详解
- php类文件的自动加载机制__autoload,spl
- 关于一次mysql的列属性not null的坑爹排查
- 机器学习的个人理解
- hdoj no.2008
- CXF wsdl2java 生成java代码供客户端使用
- spring boot 启动报A child container failed during start java.util.concurrent.ExecutionException: org.ap
- php利用SPL实现针对大文件的读取
- (4.2.42)手势解锁之九宫格控件PatternLockView
- .net 导出excel带图片
- 安装服务的命令
- Ubuntu for armel的绝唱 ubuntu-core-12.04-core-armel.tar.gz
- Java 多态
- EditCheckManager的使用
- Struts2入门之HelloWorld
- 学习:C#一次性执行多条语句(查询的是不同的表 DataTable和DataSet)