這篇文章向大家介紹php取得檔案行數的三種方法,第一種方法是使用feof和fgets函數,第二種方法是使用stream_get_line函數,第三種方法是使用count函數,具體實作請看下面實例
stream_get_line取得檔案行數
#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php
$file_path = 'xxx.txt';
$line = 0 ;
$fp = fopen ( $file_path , 'r') or die ( "open file failure!" );
if ( $fp ){
while (stream_get_line( $fp ,8192, "\n" )){
$line ++;
}
fclose( $fp );
}
echo $line ;
?>
|
登入後複製
feof和fgets取得檔案行數
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
$lines = 0;
if ( $fh = fopen ('data.txt','r')) {
while (! feof ( $fh )) {
if ( fgets ( $fh )) {
$lines ++;
}
}
}
print $lines ;
?>
|
登入後複製
count取得檔案行數
1 2 3 4 | <?php
$line = count (file('filename'));
echo $line ;
?>
|
登入後複製
第三種方式因為要保存文件的內容,效率上會很差,這裡小編推薦大家使用第一種和第二種方法。
總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。
相關推薦:
php使用html5實作多檔案上傳的方法
php實作註冊和登入介面的方法
php註冊和登入介面的實作案例
#
以上是php 實作取得檔案行數的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!