public function folder($path, $recursive = false, $types = null) { $dirs = $files = array(); if (is_dir($path)) { $cut = strlen($path); $regex = $types ? '/^.+\\.(' . $types . ')$/i' : false; $dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS); if ($recursive) { $dir = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST, RecursiveIteratorIterator::CATCH_GET_CHILD); if (is_int($recursive)) { $dir->setMaxDepth($recursive); } } foreach ($dir as $file) { $path = str_replace('\\', '/', substr($file->getRealpath(), $cut)); if ($file->isDir()) { if (iterator_count($dir->getChildren()) === 0) { rmdir($file->getRealpath()); // might as well do some garbage collection while we are at it } else { $dirs[] = $path; } } elseif ($types !== false) { if ($regex) { if (preg_match($regex, $file->getFilename())) { $files[] = $path; } } else { $files[] = $path; } } } } return array($dirs, $files); }