PHP quick reading of CSV large files by row encapsulation class sharing _ PHP Tutorial

Source: Internet
Author: User
PHP allows you to quickly read CSV large files by row. This article mainly introduces an encapsulation class for PHP to quickly read large CSV files by line. this class is also applicable to other large text files, for more information, see CSV. This article mainly introduces an encapsulation class for PHP to quickly read large CSV files by row. this class is also applicable to other large text files, for more information, see

The reading of large CSV files has been described earlier (PHP code example for reading and processing large CSV files by row ), however, there are still some problems in how to quickly and completely operate large files.

1. how to quickly obtain the total number of rows of a large CSV file?

Method 1: directly obtain the file content and use a linefeed to split the total number of rows. this method is feasible for small files and cannot be used to process large files;

Method 2: Use fgets to traverse a row to obtain the total number of rows. this method is better than the method, but large files may still time out;

Method 3: Use the SplFileObject class to directly locate the pointer to the end of the file and use the SplFileObject: key method to obtain the total number of rows. this method is feasible and efficient.

Implementation method:

The code is as follows:

$ Csv_file = 'path/bigfile.csv ';

$ Spl_object = new SplFileObject ($ csv_file, 'RB ');

$ Spl_object-> seek (filesize ($ csv_file ));

Echo $ spl_object-> key ();

2. how to quickly obtain the data of a large CSV file?

The SplFileObject class of PHP is still used for fast location through the seek method.

The code is as follows:

$ Csv_file = 'path/bigfile.csv ';

$ Start = 100000; // read from row 100,000th

$ Num = 100; // read 100 rows

$ Data = array ();

$ Spl_object = new SplFileObject ($ csv_file, 'RB ');

$ Spl_object-> seek ($ start );

While ($ num --&&! $ Spl_object-> eof ()){

$ Data [] = $ spl_object-> fgetcsv ();

$ Spl_object-> next ();

}

Print_r ($ data );

3. Combine the preceding two points into a class for reading csv files:

The code is as follows:

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;

}

}

The call method is as follows:

The code is 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 );

Echo $ line_number, chr (10 );

Print_r ($ data );

In fact, the above CsvReader class is not only for large CSV files, but also for large files or large files of other text types, provided that the fgetcsv method in the class can be slightly changed to current.

Large CSV file...

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.