-
-
Save PanjiNamjaElf/41bc81cc022e5931de2799f4e2766018 to your computer and use it in GitHub Desktop.
PHP: Check File Exists and Append Number
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
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