Example of PHP reading and processing large CSV files line by line

WBOY
Release: 2016-07-25 08:53:38
Original
1064 people have browsed it
  1. /**
  2. * csv_get_lines Read certain lines of data in the CSV file
  3. * @param $csvfile csv file path
  4. * @param $lines Read the number of lines
  5. * @param $offset The starting line number
  6. * @return array
  7. **/ bbs.it-home.org
  8. function csv_get_lines($csvfile, $lines, $offset = 0) {
  9. if(!$fp = fopen( $csvfile, 'r')) {
  10. return false;
  11. }
  12. $i = $j = 0;
  13. while (false !== ($line = fgets($fp))) {
  14. if($i++ < $offset) {
  15. continue;
  16. }
  17. break;
  18. }
  19. $data = array();
  20. while(($j++ < $lines) && !feof($fp)) {
  21. $data[] = fgetcsv( $fp);
  22. }
  23. fclose($fp);
  24. return $data;
  25. }
Copy code

Call method:

  1. $data = csv_get_lines('path/bigfile.csv', 10, 2000000);
  2. 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.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template