Welcome Guest, Not a member yet? Register   Sign In
UPLOAD IMAGE TO DATABASE
#1

[eluser]johanriyan[/eluser]
i want share upload image to database.

this is view v_upload.php

Code:
<div class="container">

&lt;?php echo $error;?&gt;

&lt;?php echo form_open_multipart('upload/do_upload');?&gt;

&lt;input type="file" name="userfile" size="20" /&gt;

<br /><br />

&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;
</div>

this is view success upload name is v_upload_success.php

Code:
<div class="container">
<h3>Your file was successfully uploaded!</h3>

<ul>
&lt;?php foreach ($upload_data as $item => $value):?&gt;
<li>&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;</li>

&lt;?php endforeach; ?&gt;
</ul>

<p>&lt;?php echo anchor('upload', 'Upload Another File!'); ?&gt;</p>
</div>

my controller

Code:
&lt;?php

class Upload extends CI_Controller {

function __construct()
{
  parent::__construct();
  $this->load->model('m_admin');
}

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

function do_upload()
{
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '1000';
  $config['max_width']  = '2000';
  $config['max_height']  = '2000';

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

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

   $this->load->view('v_upload', $error);
  }
  else
  {
   $data = array('upload_data' => $this->upload->data());
   $this->m_admin->upload($this->upload->data());
   $this->load->view('v_success_upload', $data);
  }
}
}
?&gt;


and model like this :

Code:
public function upload($image_data = array())
{
$data = array(
          'image' => $image_data['file_name']
      );
return $query=$this->db->insert('upload', $data);
}


before try, you must create table name upload
and filed image.






Theme © iAndrew 2016 - Forum software by © MyBB