• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Upload and insert file data in CodeIgniter

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;

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);
                $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.

print_r($data) to check what's in it first of all. Then build up a new array ready to insert into your db:
$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 ...


hope that's some help?


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

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


$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>';
//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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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