Rename File If Exists


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.