during my quest for some basic php knowledge , I tried to do a simple function to read files and dirs that will not use recursive iterators. (more of an exercise but actually needed for any php<5.3 servers)
the function works great - but when i tried to add a switch
($output) - it no longer lists all the files (for example, before adding the switch, it can see 61 files in 4 folders (3+root) but with the added switch
it can only see 47 (only root) .
am I doing something wrong here ? why does the switch
statement break the faux "recursive-nest" ??
function o99_list_my_files_scandir($dir,$output)
{
$result = $result2 = array();
$root = scandir($dir);
foreach($root as $value)
{
if($value === '.' || $value === '..' || $value == 'Thumb.db' || $value == 'Thumbs.db' || $value == '') { continue;}
if(is_file("$dir/$value")) {$result[]="$dir/$value"; continue;} // array files
if(is_dir("$dir/$value")) {$result2[]="$dir/$value"; continue;} // array dirs
foreach(o99_list_my_files_scandir("$dir/$value") as $value)
{
$result[] = $value;
}
}
// this is the troublemaker ...
switch ($output) {
case 'dirs' :
return array_filter($result2); // clean empty results of dirs
break;
case 'files' :
return array_filter($result); // Clean empty array of files
break;
}
}
is there any other simple way to get the function to return separate lists of dirs AND files and not a unified array ?