/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; } Example returns: myfile.jpg myfile_0.jpg myfile_1.jpg
URL: http://css-tricks.com/snippets/php/check-if-file-exists-append-number-to-name/