-
-
- /**
- * csv_get_lines Read certain lines of data in the CSV file
- * @param $csvfile csv file path
- * @param $lines Read the number of lines
- * @param $offset The starting line number
- * @return array
- **/ bbs.it-home.org
- 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;
- }
Copy code
Call method:
-
-
- $data = csv_get_lines('path/bigfile.csv', 10, 2000000);
- print_r($data);
-
Copy code
The function mainly uses line positioning The idea is to realize file pointer positioning by skipping the starting line number.
The above function has been tested on files within 500M and runs smoothly. It has not been tested on larger files. Please consider using it or improving it.
|