Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.