You are here

function icon_file_move_recursive in Icon API 8

Same name and namespace in other branches
  1. 7 includes/utilities.inc \icon_file_move_recursive()

Moves a file to a new location without database changes or hook invocation.

Parameters

string $source: The filepath or URI where the original source file(s) reside.

string $destination: The URI where $source should be moved to. This must be a stream wrapper URI. If this value is omitted, Drupal's default files scheme will be used, usually "public://".

bool|string $rename: When moving a directory, the base name of $source will be used. If desired, you can effectively rename the top level directory on $destination by providing a new base name value to the $rename parameter.

int $replace: Replace behavior when the destination file already exists: FILE_EXISTS_REPLACE - Replace the existing file (default behavior). FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique. FILE_EXISTS_ERROR - Do nothing and return FALSE.

int $depth: Internal use, do not use.

Return value

string|false The URI of the moved file or directory, or FALSE in the event of an error.

1 call to icon_file_move_recursive()
icon_provider_import_form_validate in includes/import.inc
Validate callback for 'icon_provider_import_form'.

File

includes/utilities.inc, line 317
utilities.inc Provides useful functions and common tasks.

Code

function icon_file_move_recursive($source, $destination = NULL, $rename = FALSE, $replace = FILE_EXISTS_REPLACE, $depth = 0) {
  if (is_dir(\Drupal::service("file_system")
    ->realpath($source))) {
    if (!$depth) {
      $destination .= '/' . ($rename ? $rename : \Drupal::service("file_system")
        ->basename($source));
      file_unmanaged_delete_recursive(\Drupal::service("file_system")
        ->realpath($destination));
    }
    if (!file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
      return FALSE;
    }
    $dir = dir(\Drupal::service("file_system")
      ->realpath($source));
    while (($file = $dir
      ->read()) !== FALSE) {
      if ($file == '.' || $file == '..') {
        continue;
      }
      $source_file = $source . '/' . $file;
      $destination_file = $destination . '/' . $file;
      if (!icon_file_move_recursive($source_file, $destination_file, FALSE, $replace, $depth + 1)) {
        $dir
          ->close();
        return FALSE;
      }
    }
    $dir
      ->close();
    if (!file_unmanaged_delete_recursive(\Drupal::service("file_system")
      ->realpath($source))) {
      return FALSE;
    }
    return $destination;
  }
  elseif (is_file(\Drupal::service("file_system")
    ->realpath($source))) {
    return file_unmanaged_copy($source, $destination, $replace);
  }
  return FALSE;
}