CodeIgniter Forums
Auto Rename Files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Auto Rename Files (/showthread.php?tid=12116)



Auto Rename Files - El Forum - 10-06-2008

[eluser]Unknown[/eluser]
I have a pretty simple request; though I can't seem to find any examples anywhere on the web. I'm hoping someone here knows of a simple and hopefully fast solution. I can't think of the exact words to describe this (probably aiding my difficulty in finding a solution), so my examples are below.

What I want to do is rename a file (string) automatically if a file with the same name already exists. I know I could simply just append a random number to the beginning or end of te file name and iterate until no conflict is found - simple enough, but I need a slightly better solution for aesthetic purposes.

What I want to do is similar to how Mac OS X (and Windows?) handles duplicate file names.

For example: if you duplicate a file "untitled1.jpg", it creates a file "untitled2.jpg".
If you duplicate a file "untitled3_o.jpg", it creates a file "untitled3_o2.jpg".
If you duplicate a file "untitled456_as2_a2315.jpg", it creates a file "untitled456_as2_a2316.jpg"...
...and so on...

I just can't seem to wrap my head around this. The closest I've gotten is with preg_replace(). It works to a degree, but craps out under certain conditions. Does anybody know of a simple function for this? Is there a technical term for this? Any help would be appreciated. Thank you.


Auto Rename Files - El Forum - 10-06-2008

[eluser]elvix[/eluser]
not sure if this applies in your particular situation, but the file uploading class includes a config var ('overwrite') that takes care of these situations. see the manual for more deets.

in any case, take a look thru the file uploading library, since the file-renaming code you want is in there.