PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

How to Rename when File Uploading using CodeIgniter

Posted by teejay on October 31, 2009

This would have been something I could have liked to implement when I built, Bargainph - The Buy and Sell website. I just never got to implementing this very easy feature. Instead, I have been relying on the file's filename and CodeIgniter's built-in renaming to duplicate uploaded files.

The Old-fashioned way

To my surprise, a lot of CodeIgniter users didn't think of using PHP's built-in function to rename files.
function do_upload()
{
 $file_name' = 'new_filename.ext';
 $config['upload_path'] = './uploads/';
 $config['allowed_types'] = 'gif|jpg|png';
 $config['max_size'] = '100';
 $config['max_width']  = '1024';
 $config['max_height']  = '768';
  
 $this->load->library('upload', $config);
 
 if ( ! $this->upload->do_upload())
 {
  $error = array('error' => $this->upload->display_errors());
   
  $this->load->view('upload_form', $error);
 } 
 else
 {
  $data = $this->upload->data());
  rename($file_data['full_path'], $file_data['file_path'].$file_name);
  $this->load->view('upload_success');
 }
}

Note: The script has been simplified to show how the script should work. You;ll have to check the “real” file extension, etc.

The CodeIgniter way

CodeIgniter - with the release of its 1.7.2 version - has at last created a capability to rename files on the fly.
function do_upload()
{
 $config['file_name'] = 'new_filename';
 $config['upload_path'] = './uploads/';
 $config['allowed_types'] = 'gif|jpg|png';
 $config['max_size'] = '100';
 $config['max_width']  = '1024';
 $config['max_height']  = '768';
  
 $this->load->library('upload', $config);
 
 if ( ! $this->upload->do_upload())
 {
  $error = array('error' => $this->upload->display_errors());
   
  $this->load->view('upload_form', $error);
 } 
 else
 {
  $data = array('upload_data' => $this->upload->data());
   
  $this->load->view('upload_success');
 }
}

Conclusion

This is a good feature of CodeIgniter as it reduces the time for you to parse the filename and rename - something I expect from any good PHP framework.

Categories: How To, Web Development

Tags: codeigniter, php, file uploading

5 Comments

Mark

Does the file_name config work now? I thought this did not work before.

November 1st 2009

Thorpe Obazee

@Mark. As of CI version 1.7.2, the config[‘file_name’] should be working according to the CodeIgniter Changelog.

But if you aren’t using version 1.7.2 yet, rename should suffice.

November 1st 2009

Mag

You saved my life!

November 6th 2009

Thorpe Obazee

Your welcome - I guess ;)

November 6th 2009

Daniel

Hey,

Thanks for the tutorials… very clear and helpful.

Not sure if you have checked your link to your “Bargainph” project recently, but my Kaspersky antivirus detected two trojan scripts on the page. Sorry to post here, but I could not see an email address anywhere…

If you need details then get in touch and I will email you a couple of screenshots with the details of which files were affected.

Thanks again for the tutorials

D

March 1st 2010

Comments