php高效率计算文件行数

  1. /*
  2. * 高效率计算文件行数
  3. */
  4. function count_line($file)
  5. {
  6. $fp = fopen($file, "r");
  7. $i = 0;
  8. while (!feof($fp)) {
  9. // 每次读取2M
  10. if ($data = fread($fp, 1024 * 1024 * 2)) {
  11. // 计算读取到的行数
  12. $num = substr_count($data, "\n");
  13. $i += $num;
  14. }
  15. }
  16. fclose($fp);
  17. return $i +1;
  18. }
  19. $count = count_line($filename);
  20. echo $count;
文章不错, 赏你二两银子

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续努力!