Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fgetcsv open at specific row?

Tags:

php

Is there a way to use fgetcsv to open on a particular row?

I have a really big csv and would like to run around 100 lines at a time via ajax. I can easily stop the while loop, but how would I open on a particular line? Or is this impossible?

like image 490
Somk Avatar asked Feb 17 '26 12:02

Somk


1 Answers

There's no simple way to start reading at row 100, but what you can do is read/set the position in the file like this:

$k = <previous position>;
fseek($file, $k);
while(...) {
   fgetcsv(...);
   ...
}
$k = ftell($file);   //new position

Now it's just a matter of preserving this position between page calls.

like image 110
Aleks G Avatar answered Feb 19 '26 16:02

Aleks G