php读取文件夹中的.txt文件,然后计算换行符

php read the .txt files in a folder and then count the line break

我基本上使用这段代码将行从 txt 粘贴到 php,现在它工作正常我想告诉这些行,我几乎没有发现任何与我正在做的相似的事情

<?php

                    $files = glob('../examples/upload/*.txt');
                    foreach($files as $file)
                    {
                       if(($fh = fopen($file, 'r')) != false)
                       {
                          while(!feof($fh))
                          {

                            printf("<tr><td><div>%s</div>\n", fgets($fh));
                          }
                          fclose($fh);
                       }
                    }

                  ?>   

目前此代码有效:

<?php
    $file = fopen("pages/examples/upload/cuenta1.txt", "r");
    $members = array();
      while (!feof($file)) {
        $members[] = fgets($file);
      }
    fclose($file);
    echo count($members);
?> 

但现在我需要你阅读文件夹中的所有 txt 文件并计算行数。

怎么办?试了很多方法都不行。

像第一个代码一样遍历所有文件,然后将计数相加。

$total = 0;
$files = glob('../examples/upload/*.txt');
foreach($files as $file) {
    $total += count(file($file));
}
echo $total;

file() 函数执行您的第二个代码片段执行的操作,因此您不必编写自己的循环。