CodeIgniter Forums
Example for uploading file and store data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Example for uploading file and store data (/showthread.php?tid=12766)



Example for uploading file and store data - El Forum - 10-30-2008

[eluser]Unknown[/eluser]
I am beginner with CI: i search an example code for uploading a file and store the filename into a db, with other data filed. Thank you in advance.


Example for uploading file and store data - El Forum - 10-30-2008

[eluser]manilodisan[/eluser]
Have you checked the file upload class that CI comes with?


Example for uploading file and store data - El Forum - 10-30-2008

[eluser]fesweb[/eluser]
First: read this entire page on the file uploading class in CI.

It includes this example, and notice where I've added a comment as to where you can add your database insert/update.
Code:
<?php

class Upload extends Controller {
    
    function Upload()
    {
        parent::Controller();
        $this->load->helper(array('form', 'url'));
    }
    
    function index()
    {    
        $this->load->view('upload_form', array('error' => ' ' ));
    }

    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library('upload', $config);
    
        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());
            
            $this->load->view('upload_form', $error);
        }    
        else
        {
            $data = array('upload_data' => $this->upload->data());
// the upload worked
// YOU CAN ADD THE DATABASE WORK RIGHT HERE            
// you'll have the filename and whatever other info you need available to insert into the db

            $this->load->view('upload_success', $data);
        }
    }    
}
?>