file_copy

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

Copy a file to a new location and adds a file record to the database.

This function should be used when manipulating files that have records stored in the database. This is a powerful function that in many ways performs like an advanced version of copy().

  • 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.
  • Adds the new file to the files database. If the source file is a temporary file, the resulting file will also be a temporary file.

Übergabeparameter

$source A file object.

$destination A string containing the destination that $source should be copied 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. If no database entry is found then a new one will be created.
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique.
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.

Rückgabewert

File object if the copy is successful, or FALSE in the event of an error.

See also

file_save_upload() for details on temporary files.

see file_unmanaged_copy()

hook_file_copy()

Verwandte Themen

▾ 4 functions call file_copy()

FileCopyTest::testExistingError in modules/simpletest/tests/file.test
Test that copying over an existing file fails when FILE_EXISTS_ERROR is specified.
FileCopyTest::testExistingRename in modules/simpletest/tests/file.test
Test renaming when copying over a file that already exists.
FileCopyTest::testExistingReplace in modules/simpletest/tests/file.test
Test replacement when copying over a file that already exists.
FileCopyTest::testNormal in modules/simpletest/tests/file.test
Test file copying in the normal, base case.

Code

includes/file.inc, line 385

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

  if ($filepath = file_unmanaged_copy($source->filepath, $destination, $replace)) {
    $file = clone $source;
    $file->fid = NULL;
    $file->filepath = $filepath;
    $file->filename = basename($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);
        $file->fid = $existing->fid;
        $file->filename = $existing->filename;
      }
    }
    // 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 copied.
    module_invoke_all('file_copy', $file, $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