PHP统计某个文件夹下所有文件的行数。

  <?php
   //计算某目录下指定类型文件总行数
   function get_all_files( $path ){
       $list = array();
       foreach( glob( $path . ‘/*’) as $item ){
           if( is_dir( $item ) ){
            $list = array_merge( $list , get_all_files( $item ) );
           }
           else{
            $list[] = $item;
           }
       }
       return $list;
   }
   
   function fileext($filename) {
       return trim(substr(strrchr($filename, ‘.’), 1, 10));
   }
   
   
   $filepath = dirname(__FILE__);
   $files = get_all_files($filepath);
   $countLine = $thisLine =0;
   
   //逻辑处理开始
   foreach($files as $file){
   if(fileext($file) != ‘php’){
       //echo ‘<br>file–‘.$file.’不是有效php文件,跳过’;
   } else {
       $thisLine = count(file($file));
       //echo ‘<br>file–‘.$file.’:’.$thisLine;
       $countLine+=$thisLine;
   }
   }
   echo $filepath  . “<br>———总行数count:”.$countLine;
   ?>

《PHP统计某个文件夹下所有文件的行数。》


发表评论

您的电子邮箱地址不会被公开。

18 − 8 =