file_destination

Definition

file_destination($destination, $replace)
drupal/includes/file.inc, line 490

Description

Determines the destination path for a file depending on how replacement of existing files should be handled.

Parameters

$destination A string specifying the desired path.

$replace Replace behavior when the destination file already exists.

  • FILE_EXISTS_REPLACE - Replace the existing file.
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique.
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.

Return value

The destination file path or FALSE if the file already exists and FILE_EXISTS_ERROR was specified.

Related topics

Namesort iconDescription
File interfaceCommon file handling functions.

Code

function file_destination($destination, $replace) {
  if (file_exists($destination)) {
    switch ($replace) {
      case FILE_EXISTS_REPLACE:
        // Do nothing here, we want to overwrite the existing file.
        break;

      case FILE_EXISTS_RENAME:
        $basename = basename($destination);
        $directory = dirname($destination);
        $destination = file_create_filename($basename, $directory);
        break;

      case FILE_EXISTS_ERROR:
        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' => $destination)), 'error');
        return FALSE;
    }
  }
  return $destination;
}