You are here

function file_destination in Drupal 7

Same name and namespace in other branches
  1. 8 core/includes/file.inc \file_destination()
  2. 6 includes/file.inc \file_destination()

Determines the destination path for a file.

Parameters

$destination: A string specifying the desired final URI or filepath.

$replace: Replace behavior when the destination file already exists.

Return value

The destination filepath, or FALSE if the file already exists and FILE_EXISTS_ERROR is specified.

Throws

RuntimeException Thrown if the filename contains invalid UTF-8.

Related topics

3 calls to file_destination()
FileDirectoryTest::testFileDestination in modules/simpletest/tests/file.test
This will test the filepath for a destination based on passed flags and whether or not the file exists.
file_save_upload in includes/file.inc
Saves a file upload to a new location.
file_unmanaged_copy in includes/file.inc
Copies a file to a new location without invoking the file API.

File

includes/file.inc, line 1003
API for handling file uploads and server file management.

Code

function file_destination($destination, $replace) {
  $basename = drupal_basename($destination);
  if (!drupal_validate_utf8($basename)) {
    throw new RuntimeException(sprintf("Invalid filename '%s'", $basename));
  }
  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:
        $directory = drupal_dirname($destination);
        $destination = file_create_filename($basename, $directory);
        break;
      case FILE_EXISTS_ERROR:

        // Error reporting handled by calling function.
        return FALSE;
    }
  }
  return $destination;
}