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

Asked

Viewed 1,027 times

1

I’m trying to count the number of lines a file . csv 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;
      }
    }
?>
  • 1

    which error is occurring?

  • Fatal error: Maximum Execution time of 30 Seconds exceeded in

  • already tried set_time_limit(-1); ?

2 answers

3


The problem is you’re in loop infinite; you only check if you are at the end of the file but don’t iterate. Try like this:

while (!feof($f)) {
    $linhas += 1;
    $f->next();
}

0

I managed to make a different condigo.

public function contarLinhasArquivo($arquivo){
    $linhas = 0;
    $linhas += count( file($arquivo) );
    return $linhas;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.