PHP quickly read the package class of CSV large files by line share (also applicable to other oversized text files)

Source: Internet
Author: User
Tags spl

The read of the CSV large file has been described earlier (PHP reads the code instance of the larger CSV file by line), but there are still some problems with how to quickly and fully manipulate large files.

1, how to quickly get the total number of CSV large file?

Method One: Directly obtain the contents of the file, using newline characters to split the total number of rows, this method is feasible for small files, processing large files is not feasible;
Method Two: Use Fgets row by line traversal, arrive the total number of rows, this method is better than the method, but the large file still has the possibility of timeout;
Method Three: With the help of Splfileobject class, direct pointer to the end of the file, through the Splfileobject::key method to obtain the total number of rows, this approach is feasible and efficient.

Specific implementation methods:

$csv _file = ' path/bigfile.csv '; $SPL _object New Splfileobject ($csv _file, ' RB '); $spl _object->seek (filesize($csv _file)); Echo $spl _objectkey();

2, how to quickly get the data of large CSV file?

Still using PHP's Splfileobject class, the Seek method enables quick positioning.

$csv _file= ' Path/bigfile.csv ';$start= 100000;//start reading from line 100,000th$num= 100;//Read 100 rows$data=Array();$SPL _object=NewSplfileobject ($csv _file, ' RB ');$SPL _object->seek ($start); while($num--&&!$SPL _object-EOF ()) { $data[] =$SPL _object-Fgetcsv(); $SPL _object-Next();}Print_r($data);

3, combined with the above two points, organized into a CSV file read class:

classCsvreader {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 functionSet_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 functionGet_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 =NewSplfileobject ($this->csv_file, ' RB '); }  return true; }  Public functionGet_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 functionGet_lines () {if(!$this-_open_file ()) {   return false; }  $this->spl_object->seek (filesize($this-csv_file)); return $this->spl_object->Key(); }  Public functionGet_error () {return $this-error;}}

The

method is called as follows:

 include  (' CsvReader.class.php '   $csv _file  = ' path/bigfile.csv ' ;   $csvreader  = new  Csvreader ( $csv _file  );   $line _number  =  $csvreader ->get_lines ();   $data  =  $csvreader ->get_data (10< Span style= "color: #000000;" >); echo   $line _number , chr  (10); print_r  ( $data ); 

In fact, the above Csvreader class is not only for CSV large files, for other text types of large files or oversized files are also available, provided that the class Fgetcsv method is slightly changed to current.

PHP quickly read the package class of CSV large files by line share (also applicable to other oversized text files)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.