EllisLab text mark
Advanced Search
     
Upload and insert file data in CodeIgniter
Posted: 13 October 2009 09:11 AM   [ Ignore ]
Avatar
Joined: 2007-03-21
282 posts

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

 
Posted: 13 October 2009 10:42 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2008-04-07
240 posts

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 ...

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

hope that’s some help?

 Signature 

flemmingarnott.co.uk

 
Posted: 13 October 2009 11:00 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2007-03-21
282 posts

@flemming

Hey, thanks for the reply. I just got a solution working using the following 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. :D