file_move

  1. drupal
    1. drupal6
    2. drupal7
Versionen
drupal6 file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME)
drupal7 file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)

Move 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.

see file_unmanaged_move()

Übergabeparameter

$source A file object.

$destination A string containing the destination that $source should be moved to. This can be a complete file path, a directory path or, if this value is omitted, Drupal's 'files' directory will be used.

$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 file_delete() called on the source file after hook_file_move is called. 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.

Rückgabewert

Resulting file object for success, or FALSE in the event of an error.

See also

hook_file_move()

Verwandte Themen

▾ 6 functions call file_move()

FileMoveTest::testExistingError in modules/simpletest/tests/file.test
Test that moving onto an existing file fails when FILE_EXISTS_ERROR is specified.
FileMoveTest::testExistingRename in modules/simpletest/tests/file.test
Test renaming when moving onto a file that already exists.
FileMoveTest::testExistingReplace in modules/simpletest/tests/file.test
Test replacement when moving onto a file that already exists.
FileMoveTest::testExistingReplaceSelf in modules/simpletest/tests/file.test
Test replacement when moving onto itself.
FileMoveTest::testNormal in modules/simpletest/tests/file.test
Move a normal file.
user_save in modules/user/user.module
Save changes to a user account or add a new user.

Code

includes/file.inc, line 561

<?php
function file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  $source = (object) $source;

  if ($filepath = file_unmanaged_move($source->filepath, $destination, $replace)) {
    $delete_source = FALSE;

    $file = clone $source;
    $file->filepath = $filepath;
    // If we are replacing an existing file re-use its database record.
    if ($replace == FILE_EXISTS_REPLACE) {
      $existing_files = file_load_multiple(array(), array('filepath' => $filepath));
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $delete_source = TRUE;
        $file->fid = $existing->fid;
      }
    }
    // If we are renaming around an existing file (rather than a directory),
    // use its basename for the filename.
    else if ($replace == FILE_EXISTS_RENAME && is_file(file_create_path($destination))) {
      $file->filename = basename($destination);
    }

    $file = file_save($file);

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

    if ($delete_source) {
      // Try a soft delete to remove original if it's not in use elsewhere.
      file_delete($source);
    }

    return $file;
  }
  return FALSE;
}
?>

Kommentare

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen

Kommentar hinzufügen

Der Inhalt dieses Feldes wird nicht öffentlich zugänglich angezeigt.
  • Internet- und E-Mail-Adressen werden automatisch umgewandelt.
  • Zulässige HTML-Tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Zeilen und Absätze werden automatisch erzeugt.

Weitere Informationen über Formatierungsoptionen