You are here

function file_scan_directory in Drupal 8

Same name and namespace in other branches
  1. 4 includes/file.inc \file_scan_directory()
  2. 5 includes/file.inc \file_scan_directory()
  3. 6 includes/file.inc \file_scan_directory()
  4. 7 includes/file.inc \file_scan_directory()

Finds all files that match a given mask in a given directory.

Directories and files beginning with a dot are excluded; this prevents hidden files and directories (such as SVN working directories) from being scanned. Use the umask option to skip configuration directories to eliminate the possibility of accidentally exposing configuration information. Also, you can use the base directory, recurse, and min_depth options to improve performance by limiting how much of the filesystem has to be traversed.

Parameters

$dir: The base directory or URI to scan, without trailing slash.

$mask: The preg_match() regular expression for files to be included.

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

  • 'nomask': The preg_match() regular expression for files to be excluded. Defaults to the 'file_scan_ignore_directories' setting.
  • '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 associative array of files. Possible values are 'uri', for the file's URI; 'filename', for the basename of the file; and 'name' for the name of the file without the extension. Defaults to 'uri'.
  • 'min_depth': Minimum depth of directories to return files from. Defaults to 0.

$depth: The current depth of recursion. This parameter is only used internally and should not be passed in.

Return value

An associative array (keyed on the chosen key) of objects with 'uri', 'filename', and 'name' properties corresponding to the matched files.

Deprecated

in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\File\FileSystemInterface::scanDirectory() instead.

See also

https://www.drupal.org/node/3038437

Related topics

1 call to file_scan_directory()
FileSystemDeprecationTest::testDeprecatedScanDirectory in core/tests/Drupal/KernelTests/Core/File/FileSystemDeprecationTest.php
@expectedDeprecation file_scan_directory() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\File\FileSystemInterface::scanDirectory() instead. See https://www.drupal.org/node/3038437

File

core/includes/file.inc, line 981
API for handling file uploads and server file management.

Code

function file_scan_directory($dir, $mask, $options = [], $depth = 0) {
  @trigger_error('file_scan_directory() is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Use \\Drupal\\Core\\File\\FileSystemInterface::scanDirectory() instead. See https://www.drupal.org/node/3038437', E_USER_DEPRECATED);
  $files = [];
  try {
    if (is_dir($dir)) {
      $files = \Drupal::service('file_system')
        ->scanDirectory($dir, $mask, $options);
    }
  } catch (FileException $e) {

    // Ignore and return empty array for BC.
  }
  return $files;
}