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/symfony/filesystem/Filesystem.php \Symfony\Component\Filesystem\Filesystem::copy()
  2. 11.1.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::copy()

Copies a file or directory from $source to $target.

Return value

bool

1 call to Filesystem::copy()
Filesystem::copyThenRemove in vendor/composer/composer/src/Composer/Util/Filesystem.php
Copy then delete is a non-atomic version of {@link rename}.

File

vendor/composer/composer/src/Composer/Util/Filesystem.php, line 365

Class

Filesystem
@author Jordi Boggiano <j.boggiano@seld.be> @author Johannes M. Schmitt <schmittjoh@gmail.com>

Namespace

Composer\Util

Code

public function copy(string $source, string $target) {
    // refs https://github.com/composer/composer/issues/11864
    $target = $this->normalizePath($target);
    if (!is_dir($source)) {
        try {
            return copy($source, $target);
        } catch (ErrorException $e) {
            // if copy fails we attempt to copy it manually as this can help bypass issues with VirtualBox shared folders
            // see https://github.com/composer/composer/issues/12057
            if (str_contains($e->getMessage(), 'Bad address')) {
                $sourceHandle = fopen($source, 'r');
                $targetHandle = fopen($target, 'w');
                if (false === $sourceHandle || false === $targetHandle) {
                    throw $e;
                }
                while (!feof($sourceHandle)) {
                    if (false === fwrite($targetHandle, (string) fread($sourceHandle, 1024 * 1024))) {
                        throw $e;
                    }
                }
                fclose($sourceHandle);
                fclose($targetHandle);
                return true;
            }
            throw $e;
        }
    }
    $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
    $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);
    $this->ensureDirectoryExists($target);
    $result = true;
    foreach ($ri as $file) {
        $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathname();
        if ($file->isDir()) {
            $this->ensureDirectoryExists($targetPath);
        }
        else {
            $result = $result && copy($file->getPathname(), $targetPath);
        }
    }
    return $result;
}

API Navigation

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