file_create_filename($basename, $directory)
drupal/includes/file.inc, line 659
Create a full file path from a directory and filename.
If a file with the specified name already exists, an alternative will be used.
$basename String filename
$directory String directory
File path consisting of $directory and a unique filename based off of $basename.
| Name | Description |
|---|---|
| File interface | Common file handling functions. |
function file_create_filename($basename, $directory) {
$destination = $directory . '/' . $basename;
if (file_exists($destination)) {
// Destination file already exists, generate an alternative.
$pos = strrpos($basename, '.');
if ($pos !== FALSE) {
$name = substr($basename, 0, $pos);
$ext = substr($basename, $pos);
}
else {
$name = $basename;
$ext = '';
}
$counter = 0;
do {
$destination = $directory . '/' . $name . '_' . $counter++ . $ext;
} while (file_exists($destination));
}
return $destination;
}