/** * scans a directory * * @access public * @param string $dir - directory to scan * @param boolean $with_files - list files too (true) or not (false); default: false * @param boolean $files_only - list files only (true) or not (false); default: false * @param string $remove_prefix - will be removed from the path names; default: NULL * @param array $suffixes - list of suffixes; only if $with_files = true * @param array $skip_dirs - list of directories to skip * * Examples: * - get a list of all subdirectories (no files) * $dirs = $obj->scanDirectory( <DIR> ); * * - get a list of files only * $files = $obj->scanDirectory( <DIR>, NULL, true, true ); * * - get a list of files AND directories * $list = $obj->scanDirectory( <DIR>, NULL, true ); * * - remove a path prefix * $list = $obj->scanDirectory( '/my/abs/path/to', '/my/abs/path' ); * => result is /to/subdir1, /to/subdir2, ... * **/ public static function scanDirectory($dir, $with_files = false, $files_only = false, $remove_prefix = NULL, $suffixes = array(), $skip_dirs = array(), $skip_files = array()) { $dirs = array(); $self = self::getInstance(); $self->log()->logDebug('> scanning dir: ' . $dir); if (!self::$is_win) { self::$is_win = false; if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { self::$is_win = true; } } // make sure $suffixes is an array if ($suffixes && is_scalar($suffixes)) { $suffixes = array($suffixes); } if (!count($suffixes) && count(self::$suffix_filter)) { $suffixes = self::$suffix_filter; } // make sure $skip_dirs is an array if ($skip_dirs && is_scalar($skip_dirs)) { $skip_dirs = array($skip_dirs); } if (!count($skip_dirs) && count(self::$skip_dirs)) { $skip_dirs = self::$skip_dirs; } // same for $skip_files if ($skip_files && is_scalar($skip_files)) { $skip_files = array($skip_files); } if (!count($skip_files) && count(self::$skip_files)) { $skip_files = self::$skip_files; } if (!$remove_prefix && self::$prefix) { $remove_prefix = self::$prefix; } else { $remove_orig = $remove_prefix; $remove_prefix = self::sanitizePath($remove_prefix); if ($remove_prefix == '/') { $remove_prefix = NULL; } if (substr($remove_orig, -1, 1) == '/') { $remove_prefix .= '/'; } } if (self::$current_depth > self::$max_recursion_depth) { return array(); } $self->log()->logDebug('$dir before sanitizePath: ' . $dir); $dir = self::sanitizePath($dir); $self->log()->logDebug('$dir after sanitizePath: ' . $dir); if (false !== ($dh = dir($dir))) { while (false !== ($file = $dh->read())) { $self->log()->logDebug('current directory entry: ' . $file); if (!self::$show_hidden && substr($file, 0, 1) == '.') { continue; } if (!($file == '.' || $file == '..')) { if (count($skip_dirs) && in_array(pathinfo($dir . '/' . $file, is_dir($dir . '/' . $file) ? PATHINFO_BASENAME : PATHINFO_DIRNAME), $skip_dirs)) { $self->log()->logDebug('skipping (found in $skip_dirs)'); continue; } if (count($skip_files) && in_array(pathinfo($dir . '/' . $file, PATHINFO_BASENAME), $skip_files)) { $self->log()->logDebug('skipping (found in $skip_files)'); continue; } if (is_dir($dir . '/' . $file)) { $self->log()->logDebug('It\'s a directory'); if (!$files_only) { $self->log()->logDebug("\$files_only is false, adding to \$dirs: {$dir}/{$file} - replace -{$remove_prefix}-"); $current = str_ireplace($remove_prefix, '', $dir . '/' . $file); $dirs[] = $current; } if (self::$recurse) { $self->log()->logDebug('do recursion'); // recurse self::$current_depth++; $subdirs = self::scanDirectory($dir . '/' . $file, $with_files, $files_only, $remove_prefix, $suffixes, $skip_dirs, $skip_files); $dirs = array_merge($dirs, $subdirs); self::$current_depth--; } } elseif ($with_files) { $self->log()->logDebug('It\'s a file and $with_files is true'); if (!count($suffixes) || in_array(pathinfo($file, PATHINFO_EXTENSION), $suffixes)) { $self->log()->logDebug("{$dir}/{$file} - replace -{$remove_prefix}-"); $current = str_ireplace($remove_prefix, '', $dir . '/' . $file); $dirs[] = $current; } else { $self->log()->logDebug('skipped (by suffix filter)'); } } } } $dh->close(); } else { $self->log()->logWarn('opendir failed, dir [' . $dir . ']'); } return $dirs; }