0

I currently got the following code

glob(*.php)
foreach ($files as $files..)
$content = file_get_contents($file);
$cnt=count(explode("\n", $content))
$linecount += $cnt;
echo $linecount

I have a folder with .css, .html, .js and .php files in it. Now I would like to count all the lines of code I got int he files there. How can I do this?

I found this one here How to count all the lines of code in a directory recursively?

But it didn't really help me, because it seemed like you do what they did in a Linux shell. Any ideas?

Community
  • 1
  • 1
loomie
  • 606
  • 3
  • 10
  • 21

2 Answers2

0

This script counts all lines in all files in the specified directory:

$count = 0;
$handle = opendir('./'); //Path to your directory

while (false !== ($file = readdir($handle))) {
    if($file != '.' && $file != '..' && !is_dir($file)) {
        $content = file($file);
        $count = $count + count($content);
    }
}

echo 'Lines '.$count;
alexP
  • 3,672
  • 7
  • 27
  • 36
0

Well have seen folder names like include.php so your current method would not work. You also need to check sub folders so i would advice you to use RecursiveRegexIterator :

Example

$dir = new RecursiveDirectoryIterator(__DIR__, FilesystemIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator(
         new RecursiveFileFilterIterator($dir, "/\.(css|html|js|php)$/"));

echo iterator_count($files);

Class Used

class RecursiveFileFilterIterator  extends RecursiveRegexIterator {
    public function accept() {
        return parent::accept() && is_file($this->current());
    }
}
Baba
  • 94,024
  • 28
  • 166
  • 217