Welcome Guest, Not a member yet? Register   Sign In
Upload and insert file data in CodeIgniter
#1

[eluser]doubleplusgood[/eluser]
Hi there,

I'm creating a file upload in CodeIgniter, that uploads the file to the server and then stores some data in the database.

I have a form that asks for a (friendly) file name and the file, which then uses my upload.php controller to upload the file to the server. Currently the actual upload of the file to the server is working, I just need to insert the (friendly) file name and the following from $data; file_name, file_type, file_ext and file_size.

Here is my upload frunction in the upload controller;

Code:
function do_upload()
{
        $config['upload_path'] = './gfiles/';
        $config['allowed_types'] = 'gif|jpg|jpeg|bmp|png|psd|pdf|eps|ai|zip|indd|qxt';
        $config['encrypt_name'] = 'TRUE';
        //$config['max_size']   = '100';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('upload', $error);
        }      
        else
        {
                $data = array('upload_data' => $this->upload->data());

                $this->db->insert('files', $_POST);

        }
}

Wondered if anyone has any pointers on how to get the above data into the database?

Thank you.
#2

[eluser]Flemming[/eluser]
print_r($data) to check what's in it first of all. Then build up a new array ready to insert into your db:
Code:
$insert = array(
'filename' => the_name_of_the_file from $data above,
'friendly_name' => $this->input->post('friendly_name')
);
then you can use the active record nice way to insert it all ...

Code:
$this->db->insert($insert);

hope that's some help?
#3

[eluser]doubleplusgood[/eluser]
@flemming

Hey, thanks for the reply. I just got a solution working using the following code;

Code:
$data = array('upload_data' => $this->upload->data());

$data['upload_data']['file_name'];
$data['upload_data']['file_type'];
$data['upload_data']['file_ext'];
$data['upload_data']['file_size'];

$newdb = array();
$newdb['name'] = $this->input->post('name');
$newdb['notes'] = $this->input->post('notes');
$newdb['file_name'] = $data['upload_data']['file_name'];
$newdb['file_type'] = $data['upload_data']['file_type'];
$newdb['file_ext'] = $data['upload_data']['file_ext'];
$newdb['file_size'] = $data['upload_data']['file_size'];
$newdb['owner'] = $this->auth->get_user();

//echo '<pre>';
//print_r($data);
//echo '</pre>';

//$_POST['owner'] = $this->auth->get_user();

$this->db->insert('files', $newdb);

Not sure whether this is the right way to do things as it does seem rather verbose. Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB