Created
February 22, 2012 11:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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