file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
drupal/includes/file.inc, line 427
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().
@see file_copy()
$source A string specifying the file location of the original file.
$destination A string containing the directory $source should be copied to. If this value is omitted, Drupal's 'files' directory will be used.
$replace Replace behavior when the destination file already exists:
The path to the new file, or FALSE in the event of an error.
| Name | Description |
|---|---|
| File interface | Common file handling functions. |
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;
}
// Give everyone read access so that FTP'd users or
// non-webserver users can see/read these files,
// and give group write permissions so group members
// can alter files uploaded by the webserver.
@chmod($destination, 0664);
return $destination;
}