CodeIgniter Forums
File Upload Class: Pass in new file name - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: File Upload Class: Pass in new file name (/thread-9405.html)



File Upload Class: Pass in new file name - El Forum - 06-24-2008

[eluser]louis w[/eluser]
It would be nice to be able to pass a value in to the do_upload method which is what you want the uploaded file to be named. I am saving files by their id, so need to rename it.

I can hack this by changing the name in $_FILES, but a real solution would be better.


File Upload Class: Pass in new file name - El Forum - 06-25-2008

[eluser]Chris Newton[/eluser]
Upon upload, you could rename the file, using a native PHP method.

Code:
$file_data=$this->upload->data();
rename($file_data['file_name'], 'new_name'.$file_data['file_ext']);


The upload class can rename a file to keep from overwriting an existing file anyway, so there's no fear of tacking this on, post-upload. Doing the above way is quick and easy, but might be more computationally expensive than having a config option for the upload class that does the same thing.


File Upload Class: Pass in new file name - El Forum - 06-26-2008

[eluser]jamesf[/eluser]
I've just come across the same problem, luckily I had already created a "MY_Upload" class and I simply copied over the "do_upload" method and added some code to change the filename like this:

Code:
// if a new file name was set
if($new_file_name != null) {
    $this->file_name = $new_file_name;
}

This was inserted before the method validates the filename for an existing file with the same name. Works like a charm.