note 116581 added to function.scandir

From: Date: Sat, 24 Jan 2015 16:01:56 +0000
Subject: note 116581 added to function.scandir
Groups: php.notes 
Request: Send a blank email to php-notes+get-201517@lists.php.net to get a copy of this message
Just was curious to count files and lines in a project <?php function DirLineCounter( $dir , $result = array('lines_html' => false, 'files_count' => false, 'lines_count' => false ), $complete_table = true ) { $file_read = array( 'php', 'html', 'js', 'css' ); $dir_ignore = array(); $scan_result = scandir( $dir ); foreach ( $scan_result as $key => $value ) { if ( !in_array( $value, array( '.', '..' ) ) ) { if ( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) { if ( in_array( $value, $dir_ignore ) ) { continue; } $result = DirLineCounter( $dir . DIRECTORY_SEPARATOR . $value, $result, false ); } else { $type = explode( '.', $value ); $type = array_reverse( $type ); if( !in_array( $type[0], $file_read ) ) { continue; } $lines = 0; $handle = fopen( $dir . DIRECTORY_SEPARATOR . $value, 'r' ); while ( !feof( $handle ) ) { if ( is_bool( $handle ) ) { break; } $line = fgets( $handle ); $lines++; } fclose( $handle ); $result['lines_html'][] = '<tr><td>' . $dir . '</td><td>' . $value . '</td><td>' . $lines . '</td></tr>'; $result['lines_count'] = $result['lines_count'] + $lines; $result['files_count'] = $result['files_count'] + 1; } } } if ( $complete_table ) { $lines_html = implode('', $result['lines_html']) . '<tr><td></td><td style="border: 1px solid #222">Files Total: ' . $result['files_count'] . '</td><td style="border: 1px solid #222">Lines Total: ' . $result['lines_count'] . '</td></tr>'; return '<table><tr><td style="width: 60%; background-color:#ddd;">Dir</td><td style="width: 30%; background-color:#ddd;">File</td><td style="width: 10%; background-color:#ddd;">Lines</td></tr>' . $lines_html . '</table>'; } else { return $result; } } echo DirLineCounter( '.' ); ?> ---- Server IP: 72.52.91.14 Probable Submitter: 89.11.150.216 ---- Manual Page -- http://php.net/manual/en/function.scandir.php Edit -- https://master.php.net/note/edit/116581 Del: integrated -- https://master.php.net/note/delete/116581/integrated Del: useless -- https://master.php.net/note/delete/116581/useless Del: bad code -- https://master.php.net/note/delete/116581/bad+code Del: spam -- https://master.php.net/note/delete/116581/spam Del: non-english -- https://master.php.net/note/delete/116581/non-english Del: in docs -- https://master.php.net/note/delete/116581/in+docs Del: other reasons-- https://master.php.net/note/delete/116581 Reject -- https://master.php.net/note/reject/116581 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#201517) next »