Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php读取文件夹中的.txt文件,然后计算换行次数_Php_Php 5.6 - Fatal编程技术网

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

php读取文件夹中的.txt文件,然后计算换行次数,php,php-5.6,Php,Php 5.6,我基本上是用这段代码将一行从txt粘贴到php,现在它工作得很好。我想告诉这些行,我几乎没有发现任何与我正在做的类似的东西 <?php $files = glob('../examples/upload/*.txt'); foreach($files as $file) { if(($fh = fopen($file, 'r

我基本上是用这段代码将一行从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函数执行第二个代码段所执行的操作,因此您不必编写自己的循环。

不要使用while!读取文件时的feof$fh。使用while$line=fgets$fh感谢您提供的信息,但是您知道如何说出这些行吗?非常感谢!!,我在5小时前试图解决这个问题,真的非常感谢!!!