note 87851 added to function.readdir

From: Date: Fri, 26 Dec 2008 15:24:47 +0000
Subject: note 87851 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-148530@lists.php.net to get a copy of this message
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); } ?> ---- Server IP: 195.221.21.36 Probable Submitter: 194.51.122.226 ---- Manual Page -- http://www.php.net/manual/en/function.readdir.php Edit -- https://master.php.net/note/edit/87851 Del: integrated -- https://master.php.net/note/delete/87851/integrated Del: useless -- https://master.php.net/note/delete/87851/useless Del: bad code -- https://master.php.net/note/delete/87851/bad+code Del: spam -- https://master.php.net/note/delete/87851/spam Del: non-english -- https://master.php.net/note/delete/87851/non-english Del: in docs -- https://master.php.net/note/delete/87851/in+docs Del: other reasons-- https://master.php.net/note/delete/87851 Reject -- https://master.php.net/note/reject/87851 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148530) next »