Welcome Guest, Not a member yet? Register   Sign In
File Upload - filename of uploaded files
#1

[eluser]Ornis[/eluser]
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!
#2

[eluser]drewbee[/eluser]
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.
Code:
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;
    }
    else
    {
        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.
#3

[eluser]xwero[/eluser]
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
Code:
// upload file code
$file_data = $this->upload->data();
rename($file_data['full_path'],$file_data['file_path'].$custom_name.$file_data['file_ext']);

edit : beaten Smile
#4

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

Example:
Code:
//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);
$this->upload->do_upload('upload_form_field_name');




Theme © iAndrew 2016 - Forum software by © MyBB