CodeIgniter Upload Library – Fixing Filename Iteration Limitations

I recently uncovered a seemingly undocumented (though reported here and there in forums online) issue with CodeIgniter’s built-in upload library. I’m sure there’s more than one, but the one I encountered was quite annoying…

Basically the core CodeIgniter upload library has a config setting to overwrite existing files of the same name, defaulting to false this forces the newly uploaded filename to have an integer suffix added to it before the file extension, e.g. my_data.csv would become my_data1.csv and so on if the preceding filenames exist.

Anyway, I had an application working merrily away with an upload that the client used repeatedly with files of the same name, as expected CodeIgniter happily obliged and kept uploading the files with new filenames, but this week, 2 months after the system was launched, it just stopped working. Lame.

The upload library had started throwing “The file already exists on the server”. Well, yeah, but you never cared about that before… what’s up now? After some routing around on the server I noticed that the files had got to the integer suffix of 99 – something seemed a bit neat about this so I dove into the CI_Upload class (located in system/libraries/Upload.php in the default CodeIgniter install and in the set_filename function found the following block:

As you can see the for loop is only trying to go from 1 to 99, hence why it stopped. Quite annoying that that’s not a configurable option really, so here’s a quick fix…

Before you get too enamoured with me for this solution, I have noted that the develop branch of CodeIgniter has a fix in place for this issue, so expect the next release of CodeIgniter to have the fix in place if you plan on updating (if not, this is for you!).

The Fix

What we’re going to do is make this option configurable – by no means necessary; if you wanted to you could just manually change the loop maximum, but if you were to update CodeIgniter then you’d lose your changes, so better to extend the upload library and make the option configurable. To do this we do the following.

  1. Inside application/libraries/ create a file called “MY_Upload.php”
  2. Start off the file with the usual CodeIgniter security line and create your class so that your file looks like the below:
  3. What we need to do now is rewrite the set_filename function we saw before so that we can specify the loop length as a configurable variable – what we’ll do first is add that configurable variable, so put the following just inside the class definition:
  4. Then we need to ensure that we use it, so copy the set_filename function from system/libraries/Upload.php, and change the block we looked at before so that it reads:

…and that’s it! Pretty straightforward really but if you’re unsure about extending CodeIgniter libraries then have a butchers at the documentation for doing so. You’re now all set to iterate files to your heart’s content…

Go forth and iterate.

  • Sweet fix. I guess they thought 99 iterations would have been ample? A few more 9s and you’d never have noticed the issue.

  • Rodrigo

    Could you provide this file for download? “MY_Upload.php”

    With the necessary code…

  • Why not using a while instead of a for with a maximum value?