php - using file() incrementally?

Posted by NeedBeerStat on Stack Overflow See other posts from Stack Overflow or by NeedBeerStat
Published on 2010-05-24T01:07:02Z Indexed on 2010/05/24 1:11 UTC
Read the original article Hit count: 186

Filed under:
|
|
|

I'm not sure if this is possible, I've been googling for a solution... But, essentially, I have a very large file, the lines of which I want to store in an array. Thus, I'm using file(), but is there a way to do that in batches? So that every,say, 100 lines it creates, it "pauses"?

I think there's likely to be something I can do with a foreach loop or something, but I'm not sure that I'm thinking about it the right way...

Like

$i=0;
$j=0;
$throttle=100;

foreach($files as $k => $v) {
  if($i < $j+$throttle && $i > $j) {
     $lines[] = file($v);
     //Do some other stuff, like importing into a db
  }
  $i++;
  $j++;
}

But, I think that won't really work because $i & $j will always be equal... Anyway, feeling muddled... Can someone help me think a lil' clearer?

© Stack Overflow or respective owner

Related posts about php

Related posts about file