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 containing the directory or parent URI.
Return value
File path consisting of $directory and a unique filename based off of $basename.
Related topics
Code
includes/file.inc, line 876
<?php
function file_create_filename($basename, $directory) {
// A URI or path may already have a trailing slash or look like "public://".
if (substr($directory, -1) == '/') {
$separator = '';
}
else {
$separator = '/';
}
$destination = $directory . $separator . $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 . $separator . $name . '_' . $counter++ . $ext;
} while (file_exists($destination));
}
return $destination;
}
?>Login or register to post comments 