You are here

function xmlsitemap_directory_move in XML sitemap 8

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

Move a directory to a new location.

Parameters

string $old_dir: A string specifying the filepath or URI of the original directory.

string $new_dir: A string specifying the filepath or URI of the new directory.

int $replace: Behavior when the destination file already exists. Replace behavior when the destination file already exists.

Return value

bool TRUE if the directory was moved successfully. FALSE otherwise.

1 call to xmlsitemap_directory_move()
xmlsitemap_sitemap_save in ./xmlsitemap.module
Save changes to an XML sitemap or add a new XML sitemap.

File

./xmlsitemap.module, line 561
xmlsitemap XML sitemap

Code

function xmlsitemap_directory_move($old_dir, $new_dir, $replace = FileSystemInterface::EXISTS_REPLACE) {

  /** @var \Drupal\Core\File\FileSystemInterface $filesystem */
  $filesystem = \Drupal::service('file_system');
  $success = $filesystem
    ->prepareDirectory($new_dir, $filesystem::CREATE_DIRECTORY | $filesystem::MODIFY_PERMISSIONS);
  $old_path = $filesystem
    ->realpath($old_dir);
  $new_path = $filesystem
    ->realpath($new_dir);
  if (!is_dir($old_path) || !is_dir($new_path) || !$success) {
    return FALSE;
  }
  $files = $filesystem
    ->scanDirectory($old_dir, '/.*/');
  foreach ($files as $file) {
    $file->uri_new = $new_dir . '/' . basename($file->filename);
    $success &= (bool) $filesystem
      ->move($file->uri, $file->uri_new, $replace);
  }

  // The remove the directory.
  $success &= $filesystem
    ->rmdir($old_dir);
  return $success;
}