note 89085 added to function.readdir

From: Date: Fri, 20 Feb 2009 20:11:23 +0000
Subject: note 89085 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-150905@lists.php.net to get a copy of this message
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); } ---- Server IP: 66.207.199.35 Probable Submitter: 64.180.86.111 ---- Manual Page -- http://www.php.net/manual/en/function.readdir.php Edit -- https://master.php.net/note/edit/89085 Del: integrated -- https://master.php.net/note/delete/89085/integrated Del: useless -- https://master.php.net/note/delete/89085/useless Del: bad code -- https://master.php.net/note/delete/89085/bad+code Del: spam -- https://master.php.net/note/delete/89085/spam Del: non-english -- https://master.php.net/note/delete/89085/non-english Del: in docs -- https://master.php.net/note/delete/89085/in+docs Del: other reasons-- https://master.php.net/note/delete/89085 Reject -- https://master.php.net/note/reject/89085 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150905) next »