This article mainly introduces PHP reading by row
This article mainly introduces PHP reading by row
For CSV files with millions of data records, the file size may reach several hundred mb. If it is simple to read, it is likely to time out or get stuck.
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.