file_scan_directory

Versions
4.6 – 6
file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $depth = 0)
7
file_scan_directory($dir, $mask, $options = array(), $depth = 0)

Finds all files that match a given mask in a given directory. Directories and files beginning with a period are excluded; this prevents hidden files and directories (such as SVN working directories) from being scanned.

Parameters

$dir The base directory for the scan, without trailing slash.

$mask The regular expression of the files to find.

$nomask An array of files/directories to ignore.

$callback The callback function to call for each match.

$recurse When TRUE, the directory scan will recurse the entire tree starting at the provided directory.

$key The key to be used for the returned array of files. Possible values are "filename", for the path starting with $dir, "basename", for the basename of the file, and "name" for the name of the file without an extension.

$min_depth Minimum depth of directories to return files from.

$depth Current depth of recursion. This parameter is only used internally and should not be passed.

Return value

An associative array (keyed on the provided key) of objects with "path", "basename", and "name" members corresponding to the matching files.

Related topics

▾ 9 functions call file_scan_directory()

drupal_clear_css_cache in includes/common.inc
Delete all cached CSS files.
drupal_clear_js_cache in includes/common.inc
Delete all cached JS files.
drupal_system_listing in includes/common.inc
Return an array of system file objects.
file_scan_directory in includes/file.inc
Finds all files that match a given mask in a given directory. Directories and files beginning with a period are excluded; this prevents hidden files and directories (such as SVN working directories) from being scanned.
image_get_available_toolkits in includes/image.inc
Return a list of available toolkits.
install_find_locales in ./install.php
Find all .po files for the current profile.
install_find_profiles in ./install.php
Find all .profile files.
locale_batch_by_component in includes/locale.inc
Prepare a batch to run when installing modules or enabling themes. This batch will import translations for the newly added components in all the languages already set up on the site.
locale_batch_by_language in includes/locale.inc
Prepare a batch to import translations for all enabled modules in a given language.

Code

includes/file.inc, line 890

<?php
function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $depth = 0) {
  $key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename');
  $files = array();

  if (is_dir($dir) && $handle = opendir($dir)) {
    while (FALSE !== ($file = readdir($handle))) {
      if (!in_array($file, $nomask) && $file[0] != '.') {
        if (is_dir("$dir/$file") && $recurse) {
          // Give priority to files in this folder by merging them in after any subdirectory files.
          $files = array_merge(file_scan_directory("$dir/$file", $mask, $nomask, $callback, $recurse, $key, $min_depth, $depth + 1), $files);
        }
        elseif ($depth >= $min_depth && ereg($mask, $file)) {
          // Always use this match over anything already set in $files with the same $$key.
          $filename = "$dir/$file";
          $basename = basename($file);
          $name = substr($basename, 0, strrpos($basename, '.'));
          $files[$$key] = new stdClass();
          $files[$$key]->filename = $filename;
          $files[$$key]->basename = $basename;
          $files[$$key]->name = $name;
          if ($callback) {
            $callback($filename);
          }
        }
      }
    }

    closedir($handle);
  }

  return $files;
}
?>

$files array structure

zorroposada - Tue, 2009-08-25 22:18

I think its very useful to show the structure of the array that results from this function.

Array
(
    [path/to/file.txt] => stdClass Object
        (
            [filename] => path/to/file.txt
            [basename] => file.txt
            [name] => file
        )

)

Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.