function FileRepository::copy

Same name and namespace in other branches
  1. 9 core/modules/file/src/FileRepository.php \Drupal\file\FileRepository::copy()
  2. 11.x core/modules/file/src/FileRepository.php \Drupal\file\FileRepository::copy()

Copies a file to a new location and adds a file record to the database.

This function should be used when manipulating files that have records stored in the database. 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. FileExists::Replace will error out. FileExists::Rename will rename the file until the $destination is unique.
  • Adds the new file to the files database. If the source file is a temporary file, the resulting file will also be a temporary file. See file_save_upload() for details on temporary files.

Parameters

\Drupal\file\FileInterface $source: A file entity.

string $destination: A string containing the destination that $source should be copied to. This must be a stream wrapper URI.

\Drupal\Core\File\FileExists|int $fileExists: (optional) Replace behavior when the destination file already exists.

Return value

\Drupal\file\FileInterface The file entity.

Overrides FileRepositoryInterface::copy

File

core/modules/file/src/FileRepository.php, line 138

Class

FileRepository
Provides a file entity repository.

Namespace

Drupal\file

Code

public function copy(FileInterface $source, string $destination, FileExists|int $fileExists = FileExists::Rename) : FileInterface {
  if (!$fileExists instanceof FileExists) {
    // @phpstan-ignore-next-line
    $fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
  }
  if (!$this->streamWrapperManager
    ->isValidUri($destination)) {
    throw new InvalidStreamWrapperException("Invalid stream wrapper: {$destination}");
  }
  $uri = $this->fileSystem
    ->copy($source->getFileUri(), $destination, $fileExists);
  // If we are replacing an existing file, load it.
  if ($fileExists === FileExists::Replace && ($existing = $this->loadByUri($uri))) {
    $file = $existing;
  }
  else {
    $file = $source->createDuplicate();
    $file->setFileUri($uri);
    // If we are renaming around an existing file (rather than a directory),
    // use its basename for the filename.
    if ($fileExists === FileExists::Rename && is_file($destination)) {
      $file->setFilename($this->fileSystem
        ->basename($destination));
    }
    else {
      $file->setFilename($this->fileSystem
        ->basename($uri));
    }
  }
  $file->save();
  // Inform modules that the file has been copied.
  $this->moduleHandler
    ->invokeAll('file_copy', [
    $file,
    $source,
  ]);
  return $file;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.