note 64019 deleted from ref.filesystem by felipe
| From: | felipe@php.net | Date: | Mon, 10 Dec 2007 18:00:22 +0000 |
| Subject: | note 64019 deleted from ref.filesystem by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133233@lists.php.net to get a copy of this message | ||
Note Submitter: codatrix at yahoo dot com
----
The following underlisted function called "fcountext" is a function I wrote, for counting
the number of file extensions within a given directory. Hope you find it satisfactory as there is no
alternative solution via PHP's core filesystem's functions for counting the number of file
extensions within a given directory.
<?php
/*
* Author: ORUNTA C. N.
* Email: codatrix@yahoo.com
*
* Name: fcountext()
* Function: To count the number of file extensions within a given directory.
* Argument(s): $dir, i.e. directory path - for e.g. 'htdocs/yahoo/'
* Return-type: Integer
*/
function fcountext($dir)
{
//Authenticate - if directory?
if(is_dir($dir)) {
//Open and establish directory handle/pointer
$dp = opendir($dir);
//Create extensible array
$afile = array();
//Set initializer array;
$i = 0;
//Read contents of directory into array $afile, thus:
while($content = readdir($dp)) {
//Concatenate filepath
$filepath = $dir.$content;
//Confirm any initial value within array...
if(!count($afile)) {
//Authenticate - if file?
if(is_file($filepath)) {
//Get extension using PHP's 'pathinfo'
$pathinfo = pathinfo($filepath);
//Store into the array
$afile[$i] = $pathinfo['extension'];
}
} else {
//Authenticate - if file?
if(is_file($filepath)) {
//Get extension using PHP's 'pathinfo'
$pathinfo = pathinfo($filepath);
//Count array into $len
$len = count($afile);
//Compare $pathinfo['extension'] with elements of array $afile
for($i=0; $i<$len; $i++) {
if($afile[$i] != $pathinfo['extension']) $stat = 'T';
else {
$stat = 'F';
break;
}
}
//If Status is true, then store into array
if($stat == 'T') {
$afile[$len] = $pathinfo['extension'];
}
}
}
}
//Count total no. of different extensions.
$extnum = count($afile);
//Return no. of ext.
return $extnum;
//Close directory
closedir($dp);
} else {
//Return no Value
return 'Invalid Directory';
}
}
?>
The above function can be incorporated as you deem fit into a class situation to be accessed
publicly.