function file_move

Same name in other branches
  1. 7.x includes/file.inc \file_move()
  2. 8.9.x core/modules/file/file.module \file_move()

Moves a file to a new location and update the file's database entry.

  • Checks if $source and $destination are valid and readable/writable.
  • Performs a file move if $source is not equal to $destination.
  • If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
  • Adds the new file to the files database.

Parameters

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

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

int $replace: (optional) The replace behavior when the destination file already exists. Possible values include:

Return value

\Drupal\file\FileInterface|false Resulting file entity for success, or FALSE in the event of an error.

Throws

\Drupal\Core\Entity\EntityStorageException Thrown when there is an error updating the file storage.

Deprecated

in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\file\FileRepositoryInterface::move() instead.

See also

https://www.drupal.org/node/3223520

\Drupal\file\FileRepositoryInterface::move()

\Drupal\Core\File\FileSystemInterface::move()

hook_file_move()

1 call to file_move()
LegacyFileTest::testMove in core/modules/file/tests/src/Kernel/LegacyFileTest.php
Tests the file_copy deprecation and legacy behavior.

File

core/modules/file/file.module, line 201

Code

function file_move(FileInterface $source, $destination = NULL, $replace = FileSystemInterface::EXISTS_RENAME) {
    @trigger_error(__FUNCTION__ . ' is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. Use \\Drupal\\file\\FileRepositoryInterface::move() instead. See https://www.drupal.org/node/3223520', E_USER_DEPRECATED);
    if (empty($destination)) {
        $destination = \Drupal::config('system.file')->get('default_scheme') . '://';
    }
    
    /** @var \Drupal\file\FileRepositoryInterface $file_repository */
    $file_repository = \Drupal::service('file.repository');
    try {
        return $file_repository->move($source, $destination, $replace);
    } catch (InvalidStreamWrapperException $e) {
        if (($realpath = \Drupal::service('file_system')->realpath($source->getFileUri())) !== FALSE) {
            \Drupal::logger('file')->notice('File %file (%realpath) could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', [
                '%file' => $source->getFileUri(),
                '%realpath' => $realpath,
                '%destination' => $destination,
            ]);
        }
        else {
            \Drupal::logger('file')->notice('File %file could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', [
                '%file' => $source->getFileUri(),
                '%destination' => $destination,
            ]);
        }
        \Drupal::messenger()->addError(t('The specified file %file could not be moved because the destination is invalid. More information is available in the system log.', [
            '%file' => $source->getFileUri(),
        ]));
        return FALSE;
    } catch (FileException $e) {
        return FALSE;
    }
}

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