Posted By

luman on 10/19/09


Tagged

file rename exist


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

liamchapman
BrianCoyDesign


Check if File Exists / Append Number to Name


 / Published in: PHP
 

URL: http://css-tricks.com/snippets/php/check-if-file-exists-append-number-to-name/

  1. function file_newname($path, $filename){
  2. if ($pos = strrpos($filename, '.')) {
  3. $name = substr($filename, 0, $pos);
  4. $ext = substr($filename, $pos);
  5. } else {
  6. $name = $filename;
  7. }
  8.  
  9. $newpath = $path.'/'.$filename;
  10. $newname = $filename;
  11. $counter = 0;
  12. while (file_exists($newpath)) {
  13. $newname = $name .'_'. $counter . $ext;
  14. $newpath = $path.'/'.$newname;
  15. $counter++;
  16. }
  17.  
  18. return $newname;
  19. }

Report this snippet  

You need to login to post a comment.