note 24025 deleted from function.stristr by danbrown
| From: | danbrown@php.net | Date: | Fri, 28 Nov 2008 20:09:28 +0000 |
| Subject: | note 24025 deleted from function.stristr by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-147185@lists.php.net to get a copy of this message | ||
Note Submitter: php dot net at CamelQuotesNO-SPAM-PLEASE dot com
----
Use this function to return the number of files matching a certain type (extention) in a given
folder:
Using the previous code in the function above, file extentions like .ext.inc.php will be counted as
.php files
usage:
returns false if dir doesnt exist
or returns the number of files counted
DO NOT add '.' to extention you are searching for.
example usage:
$result = CountFiles("./images", "jpg");
if($result === false) die("dir does not exist!");
function CountFiles($dir, $type)
{
if(!($dh =@opendir("$dir")))
return false; // directory does not exist
// read the directory contents searching for "type" files
// and count how many are found:
$files = 0;
while ( ! ( ($fn = readdir($dh)) === false ) )
{
$f = strrev($fn);
$ext = substr($f, 0, strpos($f,"."));
$f_ext = strrev($ext);
if(( strcasecmp($f_ext, $type) == 0 )) $files++;
}
closedir($dh);
return $files;
}