Same name and namespace in other branches
  1. 4.7.x includes/file.inc \file_move()
  2. 5.x includes/file.inc \file_move()
  3. 6.x includes/file.inc \file_move()
  4. 7.x includes/file.inc \file_move()
  5. 8.9.x core/modules/file/file.module \file_move()
  6. 9 core/modules/file/file.module \file_move()

Moves a file to a new location.

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

Parameters

$source A string specifying the file location of the original file.: This parameter will contain the resulting destination filename in case of success.

$dest A string containing the directory $source should be copied to.:

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

Return value

True for success, false for failure.

Related topics

File

includes/file.inc, line 326
API for handling file uploads and server file management.

Code

function file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) {
  $path_original = is_object($source) ? $source->filepath : $source;
  if (file_copy($source, $dest, $replace)) {
    $path_current = is_object($source) ? $source->filepath : $source;
    if ($path_original == $path_current || file_delete($path_original)) {
      return 1;
    }
    drupal_set_message(t('Removing original file failed.'), 'error');
  }
  return 0;
}