Skip to content

Instantly share code, notes, and snippets.

@glagola
Created February 22, 2012 11:38
Show Gist options
  • Save glagola/1884392 to your computer and use it in GitHub Desktop.
Save glagola/1884392 to your computer and use it in GitHub Desktop.
Generate unique name of the new file by full path to the file
<?php
function genUniqueFileName($fullPathToFile)
{
$pathInfo = pathinfo($fullPathToFile);
$k = 1;
$uniquePath = $fullPathToFile;
while (file_exists($uniquePath)) {
$uniquePath = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . '-' . $k++ . '.' . $pathInfo['extension'];
}
$newFileName = pathinfo($uniquePath, PATHINFO_BASENAME);
return $newFileName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment