Skip to content

Instantly share code, notes, and snippets.

@snowman-repos
Created October 2, 2012 08:37
Show Gist options
  • Save snowman-repos/3817385 to your computer and use it in GitHub Desktop.
Save snowman-repos/3817385 to your computer and use it in GitHub Desktop.
PHP: Check File Exists and Append Number
function file_newname($path, $filename){
if ($pos = strrpos($filename, '.')) {
$name = substr($filename, 0, $pos);
$ext = substr($filename, $pos);
} else {
$name = $filename;
}
$newpath = $path.'/'.$filename;
$newname = $filename;
$counter = 0;
while (file_exists($newpath)) {
$newname = $name .'_'. $counter . $ext;
$newpath = $path.'/'.$newname;
$counter++;
}
return $newname;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment