Skip to content

Instantly share code, notes, and snippets.

@PoTHuYJoHN
Created December 2, 2013 09:28
Show Gist options
  • Save PoTHuYJoHN/7747072 to your computer and use it in GitHub Desktop.
Save PoTHuYJoHN/7747072 to your computer and use it in GitHub Desktop.
Генерация нового названия, если файл с таким именем уже существует
<?php
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