<?php

class Upload extends CI_Controller {

	function __construct()
	{
		parent::__construct();
        $this->load->model('user_model');
        $this->load->library('session');
		$this->load->helper(array('form', 'url'));
	}

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

	function do_upload()
	{
         $user_id=$this->session->userdata('user_id');
        $p_type=$this->db->query("select * from logo where user_id='$user_id'");
          foreach ($p_type->result() as $key => $value){
               $logo=$value->logo;
          }
             
         $config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'gif|jpg|png';
                $config['max_size']             = 10000000;
                $config['max_width']            = 102400000;
                $config['max_height']           = 768000000000;

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

                if ( ! $this->upload->do_upload('userfile'))
                {
                        $error = array('error' => $this->upload->display_errors());

                        $this->load->view('upload_form', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());
                    
                    $filename=$_FILES['userfile']['name'];
            $logoname = str_replace(' ', '_', $filename);
                    $data=array(
                        'user_id'=>$user_id,
                        'logo'=>  $logoname,
                    );
                    if($logo){
                       $this->db->where('user_id', $user_id); 
                        $this->db->update('logo', $data);
                    }
                    else{
                        $this->db->insert('type', $data);
                    }
                      redirect('user/search', 'refresh');
                       // $this->load->view('upload_success', $data);
                }
        }
  //$this->db->insert('type', $data);

			
		
	
}
?>