file_create_filename

Versions
4.6 – 7
file_create_filename($basename, $directory)

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 function calls 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.

Code

includes/file.inc, line 404

<?php
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;
    }

    $counter = 0;
    do {
      $dest = $directory .'/'. $name .'_'. $counter++ . $ext;
    } while (file_exists($dest));
  }

  return $dest;
}
?>

Documentation needs expanding

allain - Fri, 2009-10-02 20:36

In the description of the function it says "an alternative will be used.", what alternative?

Short of reading the code, it's difficult to tell what the alternatives are.

Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.