function FileSystem::copy

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::copy()
  2. 10 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::copy()
  3. 11.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::copy()

Overrides FileSystemInterface::copy

File

core/lib/Drupal/Core/File/FileSystem.php, line 296

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function copy($source, $destination, $replace = self::EXISTS_RENAME) {
    $this->prepareDestination($source, $destination, $replace);
    if (!@copy($source, $destination)) {
        // If the copy failed and realpaths exist, retry the operation using them
        // instead.
        $real_source = $this->realpath($source) ?: $source;
        $real_destination = $this->realpath($destination) ?: $destination;
        if ($real_source === FALSE || $real_destination === FALSE || !@copy($real_source, $real_destination)) {
            $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}'.");
        }
    }
    // Set the permissions on the new file.
    $this->chmod($destination);
    return $destination;
}

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