function FileSystem::doScanDirectory

Same name in other branches
  1. 9 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::doScanDirectory()
  2. 10 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::doScanDirectory()
  3. 11.x 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 726

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 = [];
    // 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 (substr($dir, -1) == '/') {
                    $uri = "{$dir}{$filename}";
                }
                else {
                    $uri = "{$dir}/{$filename}";
                }
                if ($options['recurse'] && is_dir($uri)) {
                    // Give priority to files in this folder by merging them in after
                    // any subdirectory files.
                    $files = array_merge($this->doScanDirectory($uri, $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 = new \stdClass();
                    $file->uri = $uri;
                    $file->filename = $filename;
                    $file->name = pathinfo($filename, PATHINFO_FILENAME);
                    $key = $options['key'];
                    $files[$file->{$key}] = $file;
                    if ($options['callback']) {
                        $options['callback']($uri);
                    }
                }
            }
        }
        closedir($handle);
    }
    else {
        $this->logger
            ->error('@dir can not be opened', [
            '@dir' => $dir,
        ]);
    }
    return $files;
}

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