We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

section31 on 09/17/08


Tagged


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

mrjthethird
carlosabargues
jdstraughan


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.