PHP code instance _ php instance for reading and processing large CSV files by row

Source: Internet
Author: User
Tags php code examples
This article mainly introduces PHP code examples for reading and processing large CSV files by row. For more information, see CSV files with millions of data records, the file size may reach several hundred MB. if it is simple to read, it may be time-out or stuck to death.

Batch processing is necessary to successfully import the data in the CSV file into the database.

The following function reads the specified rows of data in a CSV file:

The code is as follows:


/**
* Csv_get_lines reads certain rows of data in the CSV file.
* @ Param $ csvfile csv file path
* @ Param $ lines: number of lines read
* @ Param $ Start row of offset
* @ Return array
**/
Function csv_get_lines ($ csvfile, $ lines, $ offset = 0 ){
If (! $ Fp = fopen ($ csvfile, 'r ')){
Return false;
}
$ I = $ j = 0;
While (false! ==( $ Line = fgets ($ fp ))){
If ($ I ++ <$ offset ){
Continue;
}
Break;
}
$ Data = array ();
While ($ j ++ <$ lines )&&! Feof ($ fp )){
$ Data [] = fgetcsv ($ fp );
}
Fclose ($ fp );
Return $ data;
}


Call method:

The code is as follows:


$ Data = csv_get_lines ('path/bigfile.csv ', 10,200 0000 );
Print_r ($ data );

The function uses the line locating method to locate the file pointer by skipping the starting number of lines.

The above functions have been tested for files within MB and run smoothly. for larger files, please use or improve them as appropriate.

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.