/** * \brief given a directory name, return a array of subdir paths and an array of * the files under the last subdir. * * @param string $dir * @return array ByDir, an array of arrays. * * array[dirpath]=>(array)list of files under leaf dir * * \todo test this routine with files other than the leaf dirs, does it work? * */ function filesByDir($dir) { $ByDir = array(); $fileList = array(); $subPath = ''; if (empty($dir)) { return $fileList; // nothing to process, return empty list. } try { $dirObject = new recursiveIteratorIterator(new recursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST); // dirobjs is recusiveIteratorIterator object foreach ($dirObject as $name) { $aSubPath = $dirObject->getSubPath(); /* * if we changed subpaths, we are in a new sub-dir, reset the file list */ if ($aSubPath != $subPath) { //print "DB: fileByDir: asb != sb, Init fileList!\n"; $fileList = array(); } if (is_file($name)) { $subPath = $dirObject->getSubPath(); $spn = $dirObject->getSubPathName(); $subDir = dirname($spn); if ($subDir == $aSubPath) { $fileName = $dirObject->getFilename(); $fileList[] = $fileName; } } if (empty($subPath)) { continue; } else { if (empty($fileList)) { continue; } $ByDir[$subPath] = $fileList; } /* Debug * $subPath = $dirObject->getSubPath(); print "DB: fileByDir: subpath is:$subPath\n"; $sbn = $dirObject->getSubPathName(); print "DB: fileByDir: subpathname is:$sbn\n"; $dirpath = $dirObject->getPath(); print "DB: fileByDir: dirpath is:$dirpath\n"; */ } // foreach //print "DB: fileByDir: ByDir is:\n ";print_r($ByDir) . "\n"; return $ByDir; } catch (Exception $e) { //print "in exception!\n$e\n"; return array(); } }