note 89086 added to function.readdir
| From: | Dave at osu1 dot php dot net | Date: | Fri, 20 Feb 2009 20:15:07 +0000 |
| Subject: | note 89086 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150906@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_count($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/89086
Del: integrated -- https://master.php.net/note/delete/89086/integrated
Del: useless -- https://master.php.net/note/delete/89086/useless
Del: bad code -- https://master.php.net/note/delete/89086/bad+code
Del: spam -- https://master.php.net/note/delete/89086/spam
Del: non-english -- https://master.php.net/note/delete/89086/non-english
Del: in docs -- https://master.php.net/note/delete/89086/in+docs
Del: other reasons-- https://master.php.net/note/delete/89086
Reject -- https://master.php.net/note/reject/89086
Search -- https://master.php.net/manage/user-notes.php