W3cubDocs

/Drupal 8

function file_unmanaged_move

file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)

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 $replace parameter.
  • Works around a PHP bug where rename() does not properly support streams if safe_mode or open_basedir are enabled.

Parameters

$source: A string specifying the filepath or URI of the source file.

$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 (file://) will be used. If this value is omitted, Drupal's default files scheme will be used, usually "public://".

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

Return value

The path to the new file, or FALSE in the event of an error.

See also

https://bugs.php.net/bug.php?id=60456

file_move()

Related topics

File interface
Common file handling functions.

File

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

Code

function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  if (!file_unmanaged_prepare($source, $destination, $replace)) {
    return FALSE;
  }
  // Ensure compatibility with Windows.
  // @see drupal_unlink()
  if ((substr(PHP_OS, 0, 3) == 'WIN') && (!file_stream_wrapper_valid_scheme(file_uri_scheme($source)))) {
    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 = drupal_realpath($source) ? : $source;
  $real_destination = drupal_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 drupal_unlink() as the Windows
    // issue has already been resolved above.
    if (!@copy($real_source, $real_destination) || !@unlink($real_source)) {
      \Drupal::logger('file')->error('The specified file %file could not be moved to %destination.', array('%file' => $source, '%destination' => $destination));
      return FALSE;
    }
  }
  // Set the permissions on the new file.
  drupal_chmod($destination);
  return $destination;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!includes!file.inc/function/file_unmanaged_move/8.1.x