How many lines in a file with count always returns 1
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
精彩评论