user6930268
user6930268

Reputation:

How to store the file in mysql database using codeigniter?

I have created a simple contact form.I want to store the file in database which is uploaded by any user.I am able to store the file in my folder but i have to store the file in database to identify the admin which file is upload.

Controller

public function index()
        {
                $this->load->view('demo', array('error' => ' ' ));
        }

 public function do_upload()
        {


                $id = $this->session->userdata('id');
                $this->load->model('Model_doc');

                $config['upload_path']          = './upload/';
                $config['allowed_types']        = 'docx';
                $config['max_size']             = 1000;
                $config['max_width']            = 1024;
                $config['max_height']           = 768;

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

                $this->upload->do_upload();
                $this->Model_doc->index($id, $this->upload->data());
   } 

Model

public function index($id,$imgdata)

{


 $imgdata = file_get_contents($imgdata['../upload/']);

$data = array(
        'path'=>$imgdata,

         );
    $this->db->set('curentDate', 'NOW()', FALSE);
 $this->db->insert('test',$data);

}

View

<?php echo form_open_multipart('welcome/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

Upvotes: 2

Views: 1761

Answers (1)

Ketan Solanki
Ketan Solanki

Reputation: 291

Change code in the Controller File as Folow:

public function do_upload()
    {
        $id = $this->session->userdata('id');
        $this->load->model('Model_doc');
        $config['upload_path']          = './upload/';
        $config['allowed_types']        = 'docx';
        $config['max_size']             = 1000;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;
        $this->load->library('upload', $config);
        $this->upload->do_upload();

        $full_file_path = base_url()."/upload/".$_FILES['userfile']['name'];
        //This Is Full Path of the file stored to your folder,Change it if required.

        $this->Model_doc->index($id,$full_file_path);
    }

Changes in the Model File:

public function index($id,$full_file_path)
    {

        $data = array(
            'path'=>$full_file_path,
        );
        $this->db->set('curentDate', 'NOW()', FALSE);
        $this->db->insert('test',$data);
    }

Upvotes: 1

Related Questions