Posted By

section31 on 09/17/08


Tagged


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

mrjthethird
carlosabargues
jdstraughan
fackz
vali29


Rename File If Exists


 / Published in: PHP
 

  1. function rename_if_exists($dir, $filename) {
  2. $ext = strrchr($filename, '.');
  3. $prefix = substr($filename, 0, -strlen($ext));
  4.  
  5. $i = 0;
  6. while(file_exists($dir . $filename)) { // If file exists, add a number to it.
  7. $filename = $prefix . ++$i . $ext;
  8. }
  9.  
  10. return $filename;
  11. }

Report this snippet  

You need to login to post a comment.