note 89085 deleted from function.readdir by danbrown

From: Date: Thu, 25 Feb 2010 20:33:47 +0000
Subject: note 89085 deleted from function.readdir by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166376@lists.php.net to get a copy of this message
Note Submitter: Dave ---- Both Kim Christensen and Joris DECOMBE include a $recursive var in their functions, but neither one checks for it. As a result, both functions are recursive regardless of the var setting -- and by default. Also, Joris, you assume there will be a search extension - what if we don't want to filter? Unit test, people. This version also does away with the need for static vars. /** * Return the count and filesize for files in a directory * * @param string $dir * @param boolean $recursive * @param string $searchext * @return array | false * */ function dir_count($dir, $recursive=false, $searchext=false) { if(substr($dir,-1)==".") $dir = substr($dir,0,-1); if(!is_dir($dir)) { trigger_error("Not a directory: {$dir}."); return false; } if(!$d = opendir($dir)) { trigger_error("Unable to open directory: {$dir}"); return false; } while(($i = readdir($d)) !== false) { if(($i == '.') || ($i == '..')) continue; $e = array_pop(explode(".",$i)); $p = $dir."/".$i; if(is_file($p)) { if(($searchext !== false) && ($e != $searchext)) continue; $c++; $s += filesize($p); } elseif(is_dir($p) && $recursive) { $r = dir_size($p, $recursive, $searchext); $c += $r[0]; $s += $r[1]; } } closedir($d); return array($c,$s); }

« previous php.notes (#166376) next »