Version: 1.0
Type: Function
Category: File Management
License: GNU General Public License
Description: Function that searches for certain files.
/**
* Function that searches for certain files.
*
* @param String sInitDir is the initializing directory, that's the
* directory where to start
* @param Array aExtensions is an array with all extensions that
* should be searched for
* @param Array aFiles is an array that returns all files that have
* been found.
*
* @explain First of all the function opens the initializing directory,
* then it analyses all its entries. If an entry is "." or ".." the next
* entry will be analysed. If an entry appears to be a directory, that
* directory will be analyes and so on. So basicly, this is a perfect
* example of a recursive function.
*
* @example This example searches for all text documents in your document
* root and sub directories
*
* <code>
* <?php
* $aFiles = Array();
* getFiles($_ENV['DOCUMENT_ROOT'], Array ("txt", "doc"), $aFiles);
* var_dump($aFiles);
* </code>
*
* @autor Jorgen Horstink <jorgen@webstores.nl>
*/
function getFiles($sInitDir, $aExtensions, &$aFiles)
{
$hD = dir($sInitDir);
while (false !== ($sEntry = $hD->read())) {
if ($sEntry == "." || $sEntry == "..") {
continue;
}
$sPath = $sInitDir . "/" . $sEntry;
if (!is_dir($sPath)) {
$aParts = explode(".", $sEntry);
if (in_array($aParts[sizeOf($aParts) - 1], $aExtensions)) {
$aFiles[] = $sPath;
}
} else {
getFiles($sPath, $aExtensions, $aFiles);
}
}
$hD->close();
}
?>