Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Php Programming Code Examples

Php > Directories Code Examples

GetFileList Function

GetFileList Function <?php define("BY_EXTENSION", 1); define("BY_EXPRESSION", 2); function GetFileList($HowToSearch, $Condition, $Directory, $AddPath) { $hDir = opendir($Directory); if (!$hDir) return false; $result = array(); $index = 0; // Add trailing slash to directory. if (!eregi('/${1}', $Directory)) $Directory .= "/"; // Loop while we still have directory entries. while ($dirEntry = readdir($hDir)) { $new_entry = ""; $add = false; // Add entries based on extension. if ($HowToSearch == BY_EXTENSION) if (eregi($Condition . '${1}', $dirEntry)) $add = true; // Add entries based on Perl-compatible regular-expression. if ($HowToSearch == BY_EXPRESSION) if (preg_match($Condition, $dirEntry)) $add = true; // Add the entry if it qualifies. if ($add) { if ($AddPath == true) $new_entry = $Directory; $new_entry .= $dirEntry; $result[$index++] = $new_entry; } } closedir($hDir); return $result; } // Example 1: Find files that are JPGs. $result = GetFileList(BY_EXTENSION, "jpg", "images", true); // Example 2: Find files that start with the letters 'sm_' $result = GetFileList(BY_EXPRESSION, '/^sm_/i', "images", false); ?>