You are here

function _xmlsitemap_delete_recursive in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 xmlsitemap.module \_xmlsitemap_delete_recursive()
  2. 6.2 xmlsitemap.module \_xmlsitemap_delete_recursive()
  3. 7.2 xmlsitemap.module \_xmlsitemap_delete_recursive()

Recursively delete all files and folders in the specified filepath.

This is a backport of Drupal 8's file_unmanaged_delete_recursive().

Note that this only deletes visible files with write permission.

Parameters

string $path: A filepath relative to the Drupal root directory.

bool $delete_root: A boolean if TRUE will delete the $path directory afterwards.

Return value

bool TRUE if operation was successful, FALSE otherwise.

1 call to _xmlsitemap_delete_recursive()
xmlsitemap_clear_directory in ./xmlsitemap.module
Clears sitemap directory.

File

./xmlsitemap.module, line 599
xmlsitemap XML sitemap

Code

function _xmlsitemap_delete_recursive($path, $delete_root = FALSE) {

  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');

  // Resolve streamwrapper URI to local path.
  $path = $filesystem
    ->realpath($path);
  if (is_dir($path)) {
    $dir = dir($path);
    while (($entry = $dir
      ->read()) !== FALSE) {
      if ($entry === '.' || $entry === '..') {
        continue;
      }
      $entry_path = $path . '/' . $entry;
      $filesystem
        ->deleteRecursive($entry_path);
    }
    $dir
      ->close();
    return $delete_root ? $filesystem
      ->rmdir($path) : TRUE;
  }
  return $filesystem
    ->delete($path);
}