File Upload - filename of uploaded files

Uploading a file works fine. However, I would like to give the uploaded file on the server a specific name rather than the name passed over from the source file. Can this be done within the uploading process? Thanks for CodeIgniter - it's just great!

In looking through the upload class code, you either have the option to randomize the name or use the original uploaded name. Unfortunately there doesn't appear to be way to set the filename as $this->filename is changed during the do_upload method. Extend it and do it your own way!

What I may suggset is extending the do_upload function.

Notice:: PSUDO CODE, most likely will not work. This is for a general idea of what needs to be done.
function do_upload($filename)
    $status = parent::do_upload();
    if ($status == TRUE)
        // rename previously uploaded file
        rename ($this->uploadpath . $this->filename, $this->uploadpath . $filename);
        return TRUE;
        return FALSE;

This is an easy fix to it.
Unfortunately we do double the work to complete this. (putting file then renaming). Ideally it would be better to rename it before the put. The only viable way I see doing this though is extending do_upload with practically the same code except the ability to pass a new file name to it.

At the moment there is no way to do it during the upload process AFAIK. But without changing the core files you could do something like this
// upload file code
$file_data = $this->upload->data();

edit : beaten Smile

before calling do_upload(), change 'name' element of $_FILES array to whatever you want and that'll do it.

//first get the file extension
$file_ext = explode('.', $_FILES['upload_form_field_name']['name']);
$file_ext = end($file_ext);

//overwrite original file name
$_FILES['upload_form_field_name']['name'] = $some_new_file_name.'.'.$file_ext;

//set your configs
$config['upload_path'] = '/some/path/to';

//load the lib and call do_upload()
$this->load->library('upload', $config);

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.