11-06-2009, 07:12 AM
Credits and Source: http://www.dreamincode.net/
Name: Directory Contents
Description: This snippet takes a directory and returns or excludes certain files/directories from the output.
Snippet:
Instructions: string $dir: directory you wish to search
array $ear: an array of regular expressions with what you wish to include or exclude from the results
boolean $invert: whether you wish to include or exclude TRUE = include, FALSE = exclude
integer $type: a flag setting which will include files or directories inclusively or exclusively, FILE_FLAG for files, DIR_FLAG for directories, and FILE_FLAG | DIR_FLAG for both
RETURNS
array $far: an array with files/directories
Examples:
// prints all the folders in the directory
print_r('.', getArtefacts(array('.*\.[a-z0-9]{3,4}')));
print_r(getArtefacts('.', array(), false, DIR_FLAG));
// prints all the files in the directory
print_r('.', getArtefacts(array('.*\.[a-z0-9]{3,4}'), true));
print_r(getArtefacts('.', array(), false, FILE_FLAG));
// prints all .htm or .html files in the directory
print_r('.', getArtefacts(array('.*\.html*'), true));
// prints all files that don't end in .htm or .html
print_r('.', getArtefacts(array('.*\.html*'), false, FILE_FLAG));
Thankyou for reading
Name: Directory Contents
Description: This snippet takes a directory and returns or excludes certain files/directories from the output.
Snippet:
PHP Code:
<?php
define('FILE_FLAG', 1);
define('DIR_FLAG', 2);
function getArtefacts($searchDir, $ear = array(), $invert = false, $type = 3){
$far = array();
if($handle = opendir($searchDir)){
while(false !== ($file = readdir($handle))){
$temp = false;
foreach($ear as $value){
$temp |= preg_match('/^' . $value . '$/iD', $file);
if($temp != false) break;
}
if($invert) $temp = !$temp;
$temp |= preg_match('/^\.{1,2}$/iD', $file);
if(!$temp){
switch($type){
default:
array_push($far, $file);
break;
case 2:
if(is_dir($searchDir . '/' . $file)) array_push($far, $file);
break;
case 1:
if(is_file($searchDir . '/' . $file)) array_push($far, $file);
break;
}
}
}
closedir($handle);
}
return $far;
}
?>
Instructions: string $dir: directory you wish to search
array $ear: an array of regular expressions with what you wish to include or exclude from the results
boolean $invert: whether you wish to include or exclude TRUE = include, FALSE = exclude
integer $type: a flag setting which will include files or directories inclusively or exclusively, FILE_FLAG for files, DIR_FLAG for directories, and FILE_FLAG | DIR_FLAG for both
RETURNS
array $far: an array with files/directories
Examples:
// prints all the folders in the directory
print_r('.', getArtefacts(array('.*\.[a-z0-9]{3,4}')));
print_r(getArtefacts('.', array(), false, DIR_FLAG));
// prints all the files in the directory
print_r('.', getArtefacts(array('.*\.[a-z0-9]{3,4}'), true));
print_r(getArtefacts('.', array(), false, FILE_FLAG));
// prints all .htm or .html files in the directory
print_r('.', getArtefacts(array('.*\.html*'), true));
// prints all files that don't end in .htm or .html
print_r('.', getArtefacts(array('.*\.html*'), false, FILE_FLAG));
Thankyou for reading