note 87851 deleted from function.readdir by danbrown

From: Date: Thu, 25 Feb 2010 20:33:41 +0000
Subject: note 87851 deleted from function.readdir by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166375@lists.php.net to get a copy of this message
Note Submitter: Joris DECOMBE ---- Little modification of Kim Christensen's num_files function: Added a file filter (by extension) parameter, and returns an array with the total number of files and the total size (in bytes). Example: You want to know how many .avi files you've got on a file server, and what size it costs: <?php print_r(num_files("//fileserver/share", true, "avi")) /* returns array( 0 => int(number of files), 1 => float(total size in bytes) ) */ ?> The function: <?php function num_files($dir, $recursive=false, $searchext, $counter=0, $totalsize=0) { static $counter; static $totalsize; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if ($file != "." && $file != "..") { $fileChunks = array_reverse(explode(".", $file)); $ext = $fileChunks[0]; if (is_file($dir."/".$file)&&(strtolower($ext) == strtolower($searchext))) { $counter++; $totalsize = $totalsize + filesize($dir."/".$file); } else if (is_dir($dir."/".$file)) { num_files($dir."/".$file, $recursive, $searchext, $counter); } } } closedir($dh); } } return array($counter,$totalsize); } ?>

« previous php.notes (#166375) next »