function FileSystem::doScanDirectory

Same name in other branches
  1. 9 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::doScanDirectory()
  2. 8.9.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::doScanDirectory()
  3. 10 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::doScanDirectory()

Internal function to handle directory scanning with recursion.

Parameters

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

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

array $options: The options as per ::scanDirectory().

int $depth: The current depth of recursion.

Return value

array An associative array as per ::scanDirectory().

Throws

\Drupal\Core\File\Exception\NotRegularDirectoryException If the directory does not exist.

See also

\Drupal\Core\File\FileSystemInterface::scanDirectory()

1 call to FileSystem::doScanDirectory()
FileSystem::scanDirectory in core/lib/Drupal/Core/File/FileSystem.php
Finds all files that match a given mask in a given directory.

File

core/lib/Drupal/Core/File/FileSystem.php, line 683

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

protected function doScanDirectory($dir, $mask, array $options = [], $depth = 0) {
    $files_in_sub_dirs = [];
    $files_in_this_directory = [];
    // Avoid warnings when opendir does not have the permissions to open a
    // directory.
    if ($handle = @opendir($dir)) {
        while (FALSE !== ($filename = readdir($handle))) {
            // Skip this file if it matches the nomask or starts with a dot.
            if ($filename[0] != '.' && !preg_match($options['nomask'], $filename)) {
                if (str_ends_with($dir, '/')) {
                    $uri = "{$dir}{$filename}";
                }
                else {
                    $uri = "{$dir}/{$filename}";
                }
                if ($options['recurse'] && is_dir($uri)) {
                    $files_in_sub_dirs[] = $this->doScanDirectory($uri, $mask, $options, $depth + 1);
                }
                elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {
                    // Always use this match over anything already set with the same
                    // $options['key'].
                    $file = new \stdClass();
                    $file->uri = $uri;
                    $file->filename = $filename;
                    $file->name = pathinfo($filename, PATHINFO_FILENAME);
                    $key = $options['key'];
                    $files_in_this_directory[$file->{$key}] = $file;
                    if ($options['callback']) {
                        $options['callback']($uri);
                    }
                }
            }
        }
        closedir($handle);
    }
    // Give priority to files in this folder by merging them after
    // any subdirectory files.
    return array_merge(array_merge(...$files_in_sub_dirs), $files_in_this_directory);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.