一个文件中有多少行,count总是返回1


How many lines in a file with count always returns 1

我对php非常陌生,我想知道我的文件中有多少行。我目前正在使用一些我在网上找到的代码。

$file = "filename";
$lines = count(file($file));

返回1。还有更多的台词。了解吗?这和新的行字符有关吗?我做了一个$fh = file($file); split(''n', $fh);,它的计数还是1....所以我不确定。

你可以试试:

$lines = 0;
if ($fh = fopen('file.txt', 'r')) {
  while (!feof($fh)) {
    if (fgets($fh)) {
      $lines++;
    }
  }
}
echo $lines; // line count