How to get the number of lines in a file in PHP
provides two implementation methods. Although the second one is simple and easy to understand, the first one is the most efficient
First type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
$file_path = 'xxx.txt'; //文件路径
$line = 0 ; //初始化行数
//打开文件
$fp = fopen($file_path , 'r') or die("open file failure!");
if($fp){
//获取文件的一行内容,注意:需要php5才支持该函数;
while(stream_get_line($fp,8192,"n")){
$line ;
}
fclose($fp);//关闭文件
}
//输出行数;
echo $line;
?>
|
1
2
3
4
5
1
2
3
4
|
$line = count(file('filename'));
echo $line;
?>
|
6
7
8
9
10
11
12
13
14
15
|
$file_path = 'xxx.txt'; //File path$line = 0; //Initialize the number of lines<🎜>
<🎜>//Open file<🎜>
<🎜>$fp = fopen($file_path , 'r') or die("open file failure!");<🎜>
<🎜>if($fp){<🎜>
<🎜>//Get a line of content from the file. Note: PHP5 is required to support this function; <🎜>
<🎜>while(stream_get_line($fp,8192,"n")){<🎜>
<🎜>$line ;<🎜>
<🎜>}<🎜>
<🎜>fclose($fp);//Close the file<🎜>
<🎜>}<🎜>
<🎜>//Output the number of lines; <🎜>
<🎜>echo $line;<🎜>
<🎜>?>
|
Second type:
1
2
3
4
|
<🎜>$line = count(file('filename'));<🎜>
<🎜>echo $line;<🎜>
<🎜>?>
|
The second method is very inefficient because it needs to save the contents of the file
http://www.bkjia.com/PHPjc/1014281.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1014281.htmlTechArticlePHP provides two implementation methods for obtaining the number of file lines. Although the second is simple and easy to understand, the first The first one is the most efficient: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ?php $file_path...