You are here

function media_browser_plus_move_physical_folder in Media Browser Plus 7.3

Same name and namespace in other branches
  1. 7 media_browser_plus.module \media_browser_plus_move_physical_folder()
  2. 7.2 media_browser_plus.module \media_browser_plus_move_physical_folder()

Cut-paste a directory with its children into a new filesystem location.

Parameters

string $source: The current path.

string $destination: The new path.

string $scheme: The scheme to handle.

Return value

bool TRUE on success.

1 call to media_browser_plus_move_physical_folder()
media_browser_plus_move_subfolder in includes/media_browser_plus.folders.inc
Helper function to move a subfolder - and update the related files.

File

includes/media_browser_plus.folders.inc, line 209
Folder manipulation functions.

Code

function media_browser_plus_move_physical_folder($source, $destination, $scheme = NULL) {
  if (!$scheme) {
    $scheme = variable_get('file_default_scheme', 'public');
  }
  $destination = drupal_realpath($destination);
  $source = drupal_realpath($source);
  $jail = drupal_realpath($scheme . '://');
  $files = @scandir($source);
  if ($files && count($files) > 2) {
    $transfer = new FileTransferLocal($jail);
    clearstatcache();

    // We need to copy the children first and later handle the source folder
    // since this is how FileTransferLocal works.
    foreach ($files as $file) {
      if (!in_array($file, array(
        '.',
        '..',
      ))) {
        $source_path = $source . DIRECTORY_SEPARATOR . $file;
        $copy_destination = $destination . DIRECTORY_SEPARATOR . $file;
        if (is_file($source_path)) {
          $transfer
            ->copyFile($source_path, $copy_destination);
          $transfer
            ->removeFile($source_path);
        }
        else {
          $transfer
            ->copyDirectory($source_path, $copy_destination);
          $transfer
            ->removeDirectory($source_path);
        }
      }
    }

    // All stuff is moved to destination, delete the source now.
    $transfer
      ->removeDirectory($source);
  }
  else {

    // The folder is empty so just delete and create the new one.
    if (file_exists($source)) {
      drupal_rmdir($source);
    }
    file_prepare_directory($destination, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  }
  return TRUE;
}