You are here

function file_create_filename in Drupal 6

Same name and namespace in other branches
  1. 8 core/includes/file.inc \file_create_filename()
  2. 4 includes/file.inc \file_create_filename()
  3. 5 includes/file.inc \file_create_filename()
  4. 7 includes/file.inc \file_create_filename()

Create a full file path from a directory and filename. If a file with the specified name already exists, an alternative will be used.

Parameters

$basename string filename:

$directory string directory:

Related topics

1 call to file_create_filename()
file_destination in includes/file.inc
Determines the destination path for a file depending on how replacement of existing files should be handled.

File

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

Code

function file_create_filename($basename, $directory) {
  $dest = $directory . '/' . $basename;
  if (file_exists($dest)) {

    // Destination file already exists, generate an alternative.
    if ($pos = strrpos($basename, '.')) {
      $name = substr($basename, 0, $pos);
      $ext = substr($basename, $pos);
    }
    else {
      $name = $basename;
      $ext = '';
    }
    $counter = 0;
    do {
      $dest = $directory . '/' . $name . '_' . $counter++ . $ext;
    } while (file_exists($dest));
  }
  return $dest;
}