You are here

public function S3fsFileService::copy in S3 File System 8.3

Same name and namespace in other branches
  1. 4.0.x src/S3fsFileService.php \Drupal\s3fs\S3fsFileService::copy()

Copies a file to a new location without invoking the file API.

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

  • Checks if $source and $destination are valid and readable/writable.
  • If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
  • If the $source and $destination are equal, the behavior depends on the $replace parameter. FileSystemInterface::EXISTS_REPLACE will replace the existing file. FileSystemInterface::EXISTS_ERROR will error out. FileSystemInterface::EXISTS_RENAME will rename the file until the $destination is unique.
  • Provides a fallback using realpaths if the move fails using stream wrappers. This can occur because PHP's copy() function does not properly support streams if open_basedir is enabled. See https://bugs.php.net/bug.php?id=60456

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 copied to. The URI may be a bare filepath (without a scheme).

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::copy

File

src/S3fsFileService.php, line 214

Class

S3fsFileService
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\s3fs

Code

public function copy($source, $destination, $replace = self::EXISTS_RENAME) {
  $wrapper = $this->streamWrapperManager
    ->getViaUri($destination);
  if (is_a($wrapper, 'Drupal\\s3fs\\StreamWrapper\\S3fsStream')) {
    $this
      ->prepareDestination($source, $destination, $replace);
    $srcScheme = $this->streamWrapperManager
      ->getScheme($source);
    $dstScheme = $this->streamWrapperManager
      ->getScheme($destination);
    if ($srcScheme == $dstScheme) {
      $result = $this
        ->copyObject($source, $destination);
    }
    else {
      $result = $this
        ->putObject($source, $destination);
    }
    if (!$result) {
      $this->logger
        ->error("The specified file '%source' could not be copied to '%destination'.", [
        '%source' => $source,
        '%destination' => $destination,
      ]);
      throw new FileWriteException("The specified file '{$source}' could not be copied to '{$destination}'.");
    }
    return $destination;
  }
  else {
    return $this->decorated
      ->copy($source, $destination, $replace);
  }
}