file_unmanaged_copy

  1. drupal
    1. drupal7
Versionen
drupal7 file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)

Copy a file to a new location without calling any hooks or making any changes to 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.

see file_copy()

Übergabeparameter

$source A string specifying the file location of the original file.

$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:

Rückgabewert

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

Verwandte Themen

▾ 8 functions call file_unmanaged_copy()

color_scheme_form_submit in modules/color/color.module
Submit handler for color change form.
FileUnmanagedCopyTest::testNonExistent in modules/simpletest/tests/file.test
Copy a non-existent file.
FileUnmanagedCopyTest::testNormal in modules/simpletest/tests/file.test
Copy a normal file.
FileUnmanagedCopyTest::testOverwriteSelf in modules/simpletest/tests/file.test
Copy a file onto itself.
file_copy in includes/file.inc
Copy a file to a new location and adds a file record to the database.
file_unmanaged_move in includes/file.inc
Move a file to a new location without calling any hooks or making any changes to the database.
simpletest_install in modules/simpletest/simpletest.install
Implementation of hook_install().
system_theme_settings in modules/system/system.admin.inc
Form builder; display theme configuration for entire site and individual themes.

Code

includes/file.inc, line 447

<?php
function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
  $source = realpath($source);
  if (!file_exists($source)) {
    drupal_set_message(t('The specified file %file could not be copied, because no file by that name exists. Please check that you supplied the correct filename.', array('%file' => $source)), 'error');
    return FALSE;
  }

  $destination = file_create_path($destination);
  $directory = $destination;
  $basename = file_check_path($directory);

  // Make sure we at least have a valid directory.
  if ($basename === FALSE) {
    drupal_set_message(t('The specified file %file could not be copied, because the destination %directory is not properly configured.', array('%file' => $source, '%directory' => $destination)), 'error');
    return FALSE;
  }

  // If the destination file is not specified then use the filename of the
  // source file.
  $basename = $basename ? $basename : basename($source);
  $destination = file_destination($directory . '/' . $basename, $replace);

  if ($destination === FALSE) {
    drupal_set_message(t('The specified file %file could not be copied because a file by that name already exists in the destination.', array('%file' => $source)), 'error');
    return FALSE;
  }
  // Make sure source and destination filenames are not the same, makes no
  // sense to copy it if they are. In fact copying the file will most likely
  // result in a 0 byte file. Which is bad. Real bad.
  if ($source == realpath($destination)) {
    drupal_set_message(t('The specified file %file was not copied because it would overwrite itself.', array('%file' => $source)), 'error');
    return FALSE;
  }
  if (!@copy($source, $destination)) {
    drupal_set_message(t('The specified file %file could not be copied.', array('%file' => $source)), 'error');
    return FALSE;
  }

  // Set the permissions on the new file.
  drupal_chmod($destination);

  return $destination;
}
?>

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