In terms of PHP you can do this as follows:
PHP Code:
function scanDirectory($path, $recursive=false, $conditions=array()) {
$exts = array();
$noms = array();
if(is_array($conditions)) {
foreach($conditions as $condition => $value) {
if($condition == "name") {
$array =& $noms;
} elseif($condition == "extension") {
$array =& $exts;
}
$vals = explode(",", $value);
foreach($vals as $v) {
array_push($array, $v);
}
}
}
$files = array();
if($handle = opendir($path)){
while(false !== ($file = readdir($handle))) {
if($file != "." && $file != "..") {
if(is_dir($path . "/" . $file)) {
if($recursive == true) {
$ftmp = scanDirectory($path . "/" . $file . "/", $recursive, $conditions);
if(is_array($ftmp)) {
$files = array_merge($files, $ftmp);
}
}
} else {
$ext_match = (count($exts)>0) ? false : true;
$nom_match = (count($noms)>0) ? false : true;
if($ext_match == false) {
foreach($exts as $ex) {
if(substr_count(pathinfo($path . "/" . $file, PATHINFO_EXTENSION), $ex)>0) {
$ext_match = true;
}
}
}
if($nom_match == false) {
foreach($noms as $n) {
if(substr_count($file, $n) > 0) {
$nom_match = true;
}
}
}
if($nom_match == true && $ext_match == true) {
array_push($files, $path . "/" . $file);
}
}
}
}
closedir($handle);
} else {
$files = false;
}
return $files;
}
(This is adapted from a class I wrote to deal with the Filesystem but it should work as I've presented it).
If you need an explanation of the code then ask me. Essentially it iterates through all the files in a directory (and can recursively scan directories) and it adds them to an array if they match the criteria (which are optional).
Exempli gratia:
PHP Code:
$files = scanDirectory("/path/to/my/files/", true, "extension:jpg");
foreach($files as $file) {
echo "<a href='$file' title='an image'><img src='$file' alt='image' /></a>";
}
Bookmarks