PHP and javascript snippets you can copy and paste.

Friday, March 16, 2007

dir_do_dirIteratorCallback($dir,$fn,$params)

/**
* dir_do_dirIteratorCallback
*
* Iterates over an array.
* @param string $dir - the directory to iterate over.
* @return arguments as an array.
*/

function dir_do_dirIteratorCallback($dir,$fn,$params){

$res = array();

if(class_exists('DirectoryIterator')){
$dirIterator = new DirectoryIterator($dir);
while($dirIterator->valid()) {
if(!$dirIterator->isDot()) {
if(isset($params['file'])) $params['file'] = $dirIterator->getFileName();
$res[] = call_user_func_array($fn,$params);
}
$dirIterator->next();
}
}
else{
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {

if($file!='.' && $file!='..'){
if(isset($params['file'])) $params['file'] = $file;
$res[] = call_user_func_array($fn,$params);
}
}
closedir($dh);
}
}

return $res;

}

No comments: