note 82988 added to function.readdir
| From: | KimChristensen at osu1 dot php dot net | Date: | Mon, 05 May 2008 15:14:00 +0000 |
| Subject: | note 82988 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-139071@lists.php.net to get a copy of this message | ||
Handy little function that returns the number of files (not directories) that exists under a
directory.
Choose if you want the function to recurse through sub-directories with the second parameter -
the default mode (false) is just to count the files directly under the supplied path.
<?php
/**
* Return the number of files that resides under a directory.
*
* @return integer
* @param string (required) The directory you want to start in
* @param boolean (optional) Recursive counting. Default to FALSE.
* @param integer (optional) Initial value of file count
*/
function num_files($dir, $recursive=false, $counter=0) {
static $counter;
if(is_dir($dir)) {
if($dh = opendir($dir)) {
while(($file = readdir($dh)) !== false) {
if($file != "." && $file != "..") {
$counter = (is_dir($dir."/".$file)) ? num_files($dir."/".$file, $recursive,
$counter) : $counter+1;
}
}
closedir($dh);
}
}
return $counter;
}
// Usage:
$nfiles = num_files("/home/kchr", true); // count all files that resides under
/home/kchr, including subdirs
$nfiles = num_files("/tmp"); // count the files directly under /tmp
?>
----
Server IP: 212.63.193.10
Probable Submitter: 83.140.94.180
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/82988
Del: integrated -- https://master.php.net/note/delete/82988/integrated
Del: useless -- https://master.php.net/note/delete/82988/useless
Del: bad code -- https://master.php.net/note/delete/82988/bad+code
Del: spam -- https://master.php.net/note/delete/82988/spam
Del: non-english -- https://master.php.net/note/delete/82988/non-english
Del: in docs -- https://master.php.net/note/delete/82988/in+docs
Del: other reasons-- https://master.php.net/note/delete/82988
Reject -- https://master.php.net/note/reject/82988
Search -- https://master.php.net/manage/user-notes.php