function file_move

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

Moving a file is performed by copying the file to the new location and then deleting the original.

  • 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\File $source: A file entity.

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

$replace: Replace behavior when the destination file already exists:

  • FILE_EXISTS_REPLACE - Replace the existing file. If a managed file with the destination name exists then its database entry will be updated and $source->delete() called after invoking hook_file_move(). If no database entry is found then the source files record will be updated.
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique.
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.

Return value

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

See also

file_unmanaged_move()

hook_file_move()

9 calls to file_move()
FileMoveTest::testNormal in drupal/core/modules/system/lib/Drupal/system/Tests/Image/FileMoveTest.php
Tests moving a randomly generated image.
image_field_update_field in drupal/core/modules/image/image.module
Implements hook_field_update_field().
image_field_update_instance in drupal/core/modules/image/image.module
Implements hook_field_update_instance().
MoveTest::testExistingError in drupal/core/modules/file/lib/Drupal/file/Tests/MoveTest.php
Test that moving onto an existing file fails when FILE_EXISTS_ERROR is specified.
MoveTest::testExistingRename in drupal/core/modules/file/lib/Drupal/file/Tests/MoveTest.php
Test renaming when moving onto a file that already exists.

... See full list

File

drupal/core/modules/file/file.module, line 251
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_move(File $source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  if (!file_valid_uri($destination)) {
    if (($realpath = drupal_realpath($source->uri)) !== FALSE) {
      watchdog('file', '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.', array(
        '%file' => $source->uri,
        '%realpath' => $realpath,
        '%destination' => $destination,
      ));
    }
    else {
      watchdog('file', '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.', array(
        '%file' => $source->uri,
        '%destination' => $destination,
      ));
    }
    drupal_set_message(t('The specified file %file could not be moved because the destination is invalid. More information is available in the system log.', array(
      '%file' => $source->uri,
    )), 'error');
    return FALSE;
  }
  if ($uri = file_unmanaged_move($source->uri, $destination, $replace)) {
    $delete_source = FALSE;
    $file = clone $source;
    $file->uri = $uri;

    // If we are replacing an existing file re-use its database record.
    if ($replace == FILE_EXISTS_REPLACE) {
      $existing_files = entity_load_multiple_by_properties('file', array(
        'uri' => $uri,
      ));
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $delete_source = TRUE;
        $file->fid = $existing->fid;
      }
    }
    elseif ($replace == FILE_EXISTS_RENAME && is_file($destination)) {
      $file->filename = drupal_basename($destination);
    }
    $file
      ->save();

    // Inform modules that the file has been moved.
    module_invoke_all('file_move', $file, $source);

    // Delete the original if it's not in use elsewhere.
    if ($delete_source && !file_usage()
      ->listUsage($source)) {
      $source
        ->delete();
    }
    return $file;
  }
  return FALSE;
}