You are here

public function S3fsFileService::move in S3 File System 4.0.x

Same name and namespace in other branches
  1. 8.3 src/S3fsFileService.php \Drupal\s3fs\S3fsFileService::move()

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

This is a powerful function that in many ways performs like an advanced version of rename().

  • Checks if $source and $destination are valid and readable/writable.
  • Checks that $source is not equal to $destination; if they are an error is reported.
  • If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
  • Works around a PHP bug where rename() does not properly support streams if safe_mode or open_basedir are enabled.

Parameters

string $source: A string specifying the filepath or URI of the source file.

string $destination: A URI containing the destination that $source should be moved to. The URI may be a bare filepath (without a scheme) and in that case the default scheme (public://) will be used.

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

Return value

string The path to the new file.

Throws

\Drupal\Core\File\Exception\FileException Implementation may throw FileException or its subtype on failure.

Overrides FileSystemInterface::move

See also

https://bugs.php.net/bug.php?id=60456

1 call to S3fsFileService::move()
S3fsFileService::saveData in src/S3fsFileService.php
Saves a file to the specified destination without invoking file API.

File

src/S3fsFileService.php, line 262

Class

S3fsFileService
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\s3fs

Code

public function move($source, $destination, $replace = self::EXISTS_RENAME) {
  $wrapper = $this->streamWrapperManager
    ->getViaUri($destination);
  if (is_a($wrapper, 'Drupal\\s3fs\\StreamWrapper\\S3fsStream')) {
    $this
      ->prepareDestination($source, $destination, $replace);

    // Ensure compatibility with Windows.
    // @see \Drupal\Core\File\FileSystemInterface::unlink().
    if (!$this->streamWrapperManager
      ->isValidUri($source) && substr(PHP_OS, 0, 3) == 'WIN') {
      chmod($source, 0600);
    }

    // Attempt to resolve the URIs. This is necessary in certain
    // configurations (see above) and can also permit fast moves across local
    // schemes.
    $real_source = $this
      ->realpath($source) ?: $source;
    $srcScheme = $this->streamWrapperManager
      ->getScheme($real_source);
    $dstScheme = $this->streamWrapperManager
      ->getScheme($destination);
    if ($srcScheme == $dstScheme) {
      $result = $this
        ->copyObject($real_source, $destination);
    }
    else {

      // Both sources are not on the same StreamWrapper.
      // Fall back to slow copy and unlink procedure.
      $result = $this
        ->putObject($real_source, $destination);
    }
    if (!$result) {
      $this->logger
        ->error("The specified file '%source' could not be moved to '%destination'.", [
        '%source' => $source,
        '%destination' => $destination,
      ]);
      throw new FileWriteException("The specified file '{$source}' could not be moved to '{$destination}'.");
    }
    else {
      if (!@unlink($real_source)) {
        $this->logger
          ->error("The source file '%source' could not be unlinked after copying to '%destination'.", [
          '%source' => $source,
          '%destination' => $destination,
        ]);
        throw new FileException("The source file '{$source}' could not be unlinked after copying to '{$destination}'.");
      }
    }
    return $destination;
  }
  else {
    return $this->decorated
      ->move($source, $destination, $replace);
  }
}