You are here

public static function Imce::scanDir in IMCE 8.2

Same name and namespace in other branches
  1. 8 src/Imce.php \Drupal\imce\Imce::scanDir()

Returns the contents of a directory.

1 call to Imce::scanDir()
Delete::deleteFolderUri in src/Plugin/ImcePlugin/Delete.php
Deletes a folder by uri.

File

src/Imce.php, line 247

Class

Imce
Imce container class for helper methods.

Namespace

Drupal\imce

Code

public static function scanDir($diruri, array $options = []) {
  $content = [
    'files' => [],
    'subfolders' => [],
  ];
  $browse_files = isset($options['browse_files']) ? $options['browse_files'] : TRUE;
  $browse_subfolders = isset($options['browse_subfolders']) ? $options['browse_subfolders'] : TRUE;
  if (!$browse_files && !$browse_subfolders) {
    return $content;
  }
  if (!($opendir = opendir($diruri))) {
    return $content + [
      'error' => TRUE,
    ];
  }

  // Prepare filters.
  $name_filter = empty($options['name_filter']) ? FALSE : $options['name_filter'];
  $callback = empty($options['filter_callback']) ? FALSE : $options['filter_callback'];
  $uriprefix = substr($diruri, -1) === '/' ? $diruri : $diruri . '/';
  while (($filename = readdir($opendir)) !== FALSE) {

    // Exclude special names.
    if ($filename === '.' || $filename === '..') {
      continue;
    }

    // Check filter regexp.
    if ($name_filter && preg_match($name_filter, $filename)) {
      continue;
    }

    // Check browse permissions.
    $fileuri = $uriprefix . $filename;
    $is_dir = is_dir($fileuri);
    if ($is_dir ? !$browse_subfolders : !$browse_files) {
      continue;
    }

    // Execute callback.
    if ($callback) {
      $result = $callback($filename, $is_dir, $fileuri, $options);
      if ($result === 'continue') {
        continue;
      }
      elseif ($result === 'break') {
        break;
      }
    }
    $content[$is_dir ? 'subfolders' : 'files'][$filename] = $fileuri;
  }
  closedir($opendir);
  return $content;
}