Skip to content

Instantly share code, notes, and snippets.

@benkulbertis
Created January 24, 2011 02:34
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save benkulbertis/792728 to your computer and use it in GitHub Desktop.
Checks if the file name exists, if it does uses recursion to append the file name with a number that has not yet been used.
function duplicate_fixer($fname, $count = 0, $old_fname = null){
if($count != 0){
$boom = explode(".", $old_fname);
$boom[count($boom)-2] = $boom[count($boom)-2].$count.".";
$fname = implode($boom);
}
if(file_exists($fname)){
if($count == 0) $old_fname = $fname;
$count++;
$fname = duplicate_fixer($fname, $count, $old_fname);
return $fname;
} else {
return $fname;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment