1
while ($keys = fgetcsv($fp, 0, "\t")) {

        if ($c == 0) {
            $headers = $keys;

        } else {
            $rows[] = $keys;
            //var_dump($keys);
        }

         $c ++;
    }

fclose($fp);
echo count($rows);

If I echo the $count it works fine(shows the proper count), even if I dump the keys (commented line) it echoes as expected, but nothing happens after the loop ends. at around 9154 rows;

These lines below don't work and the script execution stops without any apparent error.

fclose($fp);
echo count($rows);
rashid
  • 306
  • 3
  • 12

2 Answers2

1

Try as in the PHP manual:

while( ( $keys = fgetcsv( $fp, 0, "\t" ) ) !== FALSE ) {

Also, make sure your input csv has a clean end of file.

Jason
  • 1,987
  • 1
  • 14
  • 16
  • You need double quotes around that `\t`. – George Brighton Sep 25 '13 at 20:48
  • $data = stream_get_contents($request->getReport()); i believe then its and eof issue, tried a third party parser class, with same behavior, so I assume its an EOF issue. whats the best way to add an eof. – rashid Sep 25 '13 at 20:57
  • !== FALSE did the trick, i was fighting without knowing my options! thanks – rashid Sep 25 '13 at 21:14
  • @RashidShafique No problem, variables store boolean (true/false) as 0 or 1, so make sure you're using strict comparisons in conditions that declare/assign a variable. – Jason Sep 25 '13 at 23:58
0

Try this and tell us what happens:

// make sure we see all errors
error_reporting(-1);
ini_set('display_errors', true);

// read the entire file into an array, one element per row
$rows = array();
while($fields = fgetcsv($fp, 0, "\t")) {
    $rows[] = $fields;
}
fclose($fp);

// handle empty CSV case
if(count($rows) == 0) {
    die('CSV is empty');
}

$headers = $rows[0];
George Brighton
  • 5,131
  • 9
  • 27
  • 36
  • i tried that and it just doesnt execute beyond the fgetcsv loop, thinking its eof issue, since msw api returns data in streaming manner, its captured in $data var which I write to a file using $fp = fopen('php://memory', 'r+'); fwrite($fp, $x); rewind($fp); – rashid Sep 25 '13 at 21:00
  • Fair enough. A very low-tech way would be to copy and paste the CSV content into a new file, minus the last visible character. Then retype that character and save... – George Brighton Sep 25 '13 at 21:03