file_scan_directory

  1. drupal
    1. drupal6
    2. drupal7
Versionen
drupal6 file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse = TRUE, $key = 'filename', $min_depth = 0, $depth = 0)
drupal7 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.

Übergabeparameter

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

$mask The preg_match() regular expression of the files to find.

$options An associative array of additional options, with the following keys:

  • 'nomask' The preg_match() regular expression of the files to ignore. Defaults to '/(\.\.?|CVS)$/'.
  • 'callback' The callback function to call for each match. There is no default callback.
  • 'recurse' When TRUE, the directory scan will recurse the entire tree starting at the provided directory. Defaults to TRUE.
  • 'key' The key to be used for the returned array of files. Possible values are 'filepath', for the path starting with $dir, 'filename', for the basename of the file, and 'name' for the name of the file without an extension. Defaults to 'filepath'.
  • 'min_depth' Minimum depth of directories to return files from. Defaults to 0.

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

Rückgabewert

An associative array (keyed on the provided key) of objects with 'filepath', 'filename', and 'name' members corresponding to the matching files.

Verwandte Themen

▾ 20 functions call file_scan_directory()

drupalGetTestFiles in modules/simpletest/drupal_web_test_case.php
Get a list files that can be used in tests.
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_detect_database_types in includes/install.inc
Detect all supported databases that are compiled into PHP.
drupal_system_listing in includes/common.inc
Return an array of system file objects.
FileScanDirectoryTest::testOptionCallback in modules/simpletest/tests/file.test
Check that the callback function is called correctly.
FileScanDirectoryTest::testOptionKey in modules/simpletest/tests/file.test
Check that key parameter sets the return value's key.
FileScanDirectoryTest::testOptionMinDepth in modules/simpletest/tests/file.test
Check that the min_depth options lets us ignore files in the starting directory.
FileScanDirectoryTest::testOptionNoMask in modules/simpletest/tests/file.test
Check that the no-mask parameter is honored.
FileScanDirectoryTest::testOptionRecurse in modules/simpletest/tests/file.test
Check that the recurse option decends into subdirectories.
FileScanDirectoryTest::testReturn in modules/simpletest/tests/file.test
Check the format of the returned values.
file_scan_directory in includes/file.inc
Finds all files that match a given mask in a given directory.
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.
simpletest_get_all_tests in modules/simpletest/simpletest.module
Get a list of all of the tests.
simpletest_install in modules/simpletest/simpletest.install
Implementation of hook_install().
simpletest_uninstall in modules/simpletest/simpletest.install
Implementation of hook_uninstall().
_registry_rebuild in includes/registry.inc

Code

includes/file.inc, line 1406

<?php
function file_scan_directory($dir, $mask, $options = array(), $depth = 0) {
  // Merge in defaults.
  $options += array(
    'nomask' => '/(\.\.?|CVS)$/',
    'callback' => 0,
    'recurse' => TRUE,
    'key' => 'filepath',
    'min_depth' => 0,
  );

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

    closedir($handle);
  }

  return $files;
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen