I’m working on a CSV upload that has 35,000 lines of stuff going on and I’m trying to think of a way to start my parsing of the CSV file at line “X”. I’ve been refreshing the page and saving the line I was last on each time so it would fix the timeout issue I was having with browsers. Now, I just need to know how to start my CSV file on that line I left off.
while(($data = fgetcsv($handle, 0, ",")) !== false) {
if($i > 2000) { break; }
//start parsing here
$i++
}
$_SESSION['lastLine'] = $i;
$_SESSION['filepath'] = $filepath;
//resend headers
I save the filepath as a session as well because it’s been uploaded to a temporary folder and I can re-access it. So the first time I upload, I go through all 2000 lines perfectly happy. The headers reset and start the process again, but since $_SESSION[‘lastline’] is now 2001, the code breaks (which is fine). What I need to figure out is how can I start my parsing on line 2001 and break again on 4000. This will continue until all 35,000 lines are done.
I was thinking about setting the break statement before the parsing to say:
if($i < $_SESSION['lastLine']) { $i++; }
if($i > $_SESSION['lastLine'] + 2000) { break; }
My problem is, doing 35,000 lines of CSV means that the last 10 or so runs will have to do an i++ loop thousands of times before the parsing even begins. I looked around for some ways to just start the CSV file at line “X”, which would be my preferred method of doing this, but I couldn’t find anything. Any help would be awesome.
Use
ftell()to save your position within the file. You can thenfseek()to that position before restarting your fgetcsv() loop, e.g.