For millions of data CSV files, the file size may reach hundreds of M, and if a simple read is likely to be timed out or stuck to death.
Batch processing is necessary to successfully import data from a CSV file into a database.
The following function reads a few rows of data specified in a CSV file:
Copy Code code as follows:
/**
* Csv_get_lines read a few rows of data in a CSV file
* @param $csvfile CSV file path
* @param number of rows $lines read
* @param $offset number of starting lines
* @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:
Copy Code code as follows:
$data = Csv_get_lines (' path/bigfile.csv ', 10, 2000000);
Print_r ($data);
The function mainly uses the line localization idea, by skipping the starting line number to realize the file pointer localization.
The above function of 500M of files have been tested, running smoothly, for the larger files did not test, please use or improve.