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.