function FileSystem::move

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

Moves a file to a new location without database changes or hook invocation.

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

  • Checks if $source and $destination are valid and readable/writable.
  • Checks that $source is not equal to $destination; if they are an error is reported.
  • If file already exists in $destination either the call will error out, replace the file or rename the file based on the $fileExists parameter.
  • Works around a PHP bug where rename() does not properly support streams if safe_mode or open_basedir are enabled.

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 moved to. The URI may be a bare filepath (without a scheme) and in that case the default scheme (public://) will be used.

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

Return value

string The path to the new file.

Overrides FileSystemInterface::move

1 call to FileSystem::move()
FileSystem::saveData in core/lib/Drupal/Core/File/FileSystem.php
Saves a file to the specified destination without invoking file API.

File

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

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function move($source, $destination, $fileExists = FileExists::Rename) {
  if (!$fileExists instanceof FileExists) {
    // @phpstan-ignore-next-line
    $fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
  }
  $this->prepareDestination($source, $destination, $fileExists);
  // Ensure compatibility with Windows.
  // @see \Drupal\Core\File\FileSystemInterface::unlink().
  if (!$this->streamWrapperManager
    ->isValidUri($source) && str_starts_with(PHP_OS, 'WIN')) {
    chmod($source, 0600);
  }
  // Attempt to resolve the URIs. This is necessary in certain
  // configurations (see above) and can also permit fast moves across local
  // schemes.
  $real_source = $this->realpath($source) ?: $source;
  $real_destination = $this->realpath($destination) ?: $destination;
  // Perform the move operation.
  if (!@rename($real_source, $real_destination)) {
    // Fall back to slow copy and unlink procedure. This is necessary for
    // renames across schemes that are not local, or where rename() has not
    // been implemented. It's not necessary to use FileSystem::unlink() as the
    // Windows issue has already been resolved above.
    if (!@copy($real_source, $real_destination)) {
      throw new FileWriteException("The specified file '{$source}' could not be moved to '{$destination}'.");
    }
    if (!@unlink($real_source)) {
      throw new FileException("The source file '{$source}' could not be unlinked after copying 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.