Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Filesystem.php

function Filesystem::copy

Same name in this branch
  1. 11.1.x vendor/composer/composer/src/Composer/Util/Filesystem.php \Composer\Util\Filesystem::copy()
  2. 11.1.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::copy()

Copies a file.

If the target file is older than the origin file, it's always overwritten. If the target file is newer, it is overwritten only when the $overwriteNewerFiles option is set to true.

Throws

FileNotFoundException When originFile doesn't exist

IOException When copy fails

1 call to Filesystem::copy()
Filesystem::mirror in vendor/symfony/filesystem/Filesystem.php
Mirrors a directory to another.

File

vendor/symfony/filesystem/Filesystem.php, line 37

Class

Filesystem
Provides basic utility to manipulate the file system.

Namespace

Symfony\Component\Filesystem

Code

public function copy(string $originFile, string $targetFile, bool $overwriteNewerFiles = false) : void {
    $originIsLocal = stream_is_local($originFile) || 0 === stripos($originFile, 'file://');
    if ($originIsLocal && !is_file($originFile)) {
        throw new FileNotFoundException(\sprintf('Failed to copy "%s" because file does not exist.', $originFile), 0, null, $originFile);
    }
    $this->mkdir(\dirname($targetFile));
    $doCopy = true;
    if (!$overwriteNewerFiles && !parse_url($originFile, \PHP_URL_HOST) && is_file($targetFile)) {
        $doCopy = filemtime($originFile) > filemtime($targetFile);
    }
    if ($doCopy) {
        // https://bugs.php.net/64634
        if (!($source = self::box('fopen', $originFile, 'r'))) {
            throw new IOException(\sprintf('Failed to copy "%s" to "%s" because source file could not be opened for reading: ', $originFile, $targetFile) . self::$lastError, 0, null, $originFile);
        }
        // Stream context created to allow files overwrite when using FTP stream wrapper - disabled by default
        if (!($target = self::box('fopen', $targetFile, 'w', false, stream_context_create([
            'ftp' => [
                'overwrite' => true,
            ],
        ])))) {
            throw new IOException(\sprintf('Failed to copy "%s" to "%s" because target file could not be opened for writing: ', $originFile, $targetFile) . self::$lastError, 0, null, $originFile);
        }
        $bytesCopied = stream_copy_to_stream($source, $target);
        fclose($source);
        fclose($target);
        unset($source, $target);
        if (!is_file($targetFile)) {
            throw new IOException(\sprintf('Failed to copy "%s" to "%s".', $originFile, $targetFile), 0, null, $originFile);
        }
        if ($originIsLocal) {
            // Like `cp`, preserve executable permission bits
            self::box('chmod', $targetFile, fileperms($targetFile) | fileperms($originFile) & 0111);
            // Like `cp`, preserve the file modification time
            self::box('touch', $targetFile, filemtime($originFile));
            if ($bytesCopied !== ($bytesOrigin = filesize($originFile))) {
                throw new IOException(\sprintf('Failed to copy the whole content of "%s" to "%s" (%g of %g bytes copied).', $originFile, $targetFile, $bytesCopied, $bytesOrigin), 0, null, $originFile);
            }
        }
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal