Welcome Guest, Not a member yet? Register   Sign In
Example for uploading file and store data
#1

[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.
#2

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

[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);
        }
    }    
}
?>




Theme © iAndrew 2016 - Forum software by © MyBB