0

I want to count number of files in a php file. I am using CodeIgniter framework. Currently I have tried this below

$file='contractor.php';
$mypath= 'application/controller/'.$file;
$linecount = 0;
$handle = fopen($mypath, "r");
while(!feof($handle)){
    $line = fgets($handle);
    $linecount++;
}
fclose($handle);

echo $linecount;

Currently after executing the file in a function it keeps loading. i want to find number of lines in the file.like output: 202

Airn5475
  • 2,452
  • 29
  • 51

4 Answers4

1

Try this one hope this will work for you :)

$file = 'getinvoice'; 
no_of_lines = count(file($file)); 
echo "number of lines $file";
0

Your code looks good.

I suppose that the function keeps loading because the file is too big.

Check this answer for an efficient approach to the problem: Efficiently counting the number of lines of a text file. (200mb+)

Stefano
  • 179
  • 2
  • 13
0
$file = basename($_SERVER['PHP_SELF']);
 
$no_of_lines = count(file($file));
 
echo "There are $no_of_lines lines in $file"."\n";
meewog
  • 1,690
  • 1
  • 22
  • 26
-1

If your files are quite small, you could try something like this.

$lines=file($filename);
$count=count($lines);
Graham
  • 21
  • 3