<?php
/**
- Created by PhpStorm.
- User: hihone
- Date: 2018/4/10
- Time: 12:02
- Description: 读取文件总行数
- 如果有需排除统计的文件,可以自行修改
/
$line = 0;
$dir = DIR;
$dir_file = glob('');
$disabledList = ['a.php', 'news'];//排除统计文件或目录
foreach ($dir_file as $v) {
$line = listDirOrFile($dir . '/' . $v, $line);
}
function listDirOrFile($dir_file_name, $line) {
if (is_dir($dir_file_name)) {
$dir_file = glob($dir_file_name . '/');
if ($dir_file) {
foreach ($dir_file as $val) {
if (is_dir($dir_file_name . '/' . $val)) {
listDirOrFile($dir_file_name . '/' . $val, $line);
} else {
$line = countLine($val, $line);
}
}
}
} else {
$line = countLine($dir_file_name, $line);
}
return $line;
}
function countLine($file, $line) {
$fp=fopen($file, "r");
while(!feof($fp)) {
//每次读取2M
if($data=fread($fp,10241024*2)){
//计算读取到的行数
$num=substr_count($data,"\n");
$line+=$num;
}
}
fclose($fp);
return $line;
}
echo '项目总共有 ' . $line . ' 行';
网友评论