开发者

How many lines in a file with count always returns 1

开发者 https://www.devze.com 2023-03-28 05:42 出处:网络
I\'m extremely new to php and I am trying to get how many lines there are in my file.I am currently using some code I found online.

I'm extremely new to php and I am trying to get how many lines there are in my file. I am currently using some code I found online.

$file = "filename";
$lines = count(file($file));
开发者_JAVA技巧

This returns 1. and there are MANY more lines than that. Any insight? Does it have something to do with new line characters? I did a $fh = file($file); split('\n', $fh); and the count of that is still 1....so I am not sure.


You can try this :

$lines = 0;

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号