stream_get_line獲取文件行數(shù)
<?php $file_path = 'xxx.txt'; //文件路徑 $line = 0 ; //初始化行數(shù) //打開(kāi)文件 $fp = fopen($file_path , 'r') or die("open file failure!"); if($fp){ //獲取文件的一行內(nèi)容,注意:需要php5才支持該函數(shù); while(stream_get_line($fp,8192,"/n")){ $line++; } fclose($fp);//關(guān)閉文件 } //輸出行數(shù); echo $line; ?> feof和fgets獲取文件行數(shù)
<?php$lines = 0;//初始化行數(shù) if ($fh = fopen('data.txt','r')) {//打開(kāi)文件 while (! feof($fh)) {//判斷是否已經(jīng)達(dá)到文件底部 if (fgets($fh)) {//讀取一行內(nèi)容 $lines++; } }}print $lines;?> count獲取文件行數(shù)
<?php $line = count(file('filename')); echo $line; ?> 第三種方式因?yàn)橐4嫖募膬?nèi)容,效率上會(huì)很差,這里小編推薦大家使用第一種和第二種方法。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選