How to count the number of rows in a .csv file

1

I'm trying to count the number of rows in a .csv file but giving error

  

Fatal error: Maximum execution time of 30 seconds exceeded in.

<?php
  ini_set('max_execution_time', 300);

  class Teste{
      public function contarLinhas($arquivo){
          $linhas = 0;
          if($f = fopen($arquivo, "r" )){
              while (!feof($f)) {
                  $linhas += 1;
              }
          }
          return $linhas;
      }
    }
?>
    
asked by anonymous 04.01.2018 / 15:26

2 answers

3

The problem is that it is in loop infinity; you only check if it is at the end of the file but it does not iterate. Try this:

while (!feof($f)) {
    $linhas += 1;
    $f->next();
}
    
04.01.2018 / 15:44
1

I managed to make a different track.

public function contarLinhasArquivo($arquivo){
    $linhas = 0;
    $linhas += count( file($arquivo) );
    return $linhas;
}
    
05.01.2018 / 14:27