Welcome Guest, Not a member yet? Register   Sign In
upload image using model/view/controller
#1

[eluser]Unknown[/eluser]
I want to need help to upload image file.B'coz i had tried simply upload file but how to save that file in database table using model file.....
#2

[eluser]Pascal Kriete[/eluser]
You need to upload it to a temporary spot on the disk first. Then you can do something like this:
Code:
// Get the image as 'text'
$image = file_get_contents('path/to/image.png');

$data = array(
    'name' => 'image.png',
    'contents' => $image
);

$this->db->insert('images', $data);

The contents field should be of the 'blob' or 'largeblob' type. Also, iirc file_get_contents reads the whole file into memory, so if you expect a lot of uploads you may want to consider creating a queue of sorts. And always check the file size to make sure you don't go over your memory limit.

Putting the file somewhere on the disk and just saving the path to the db is the solution I would use. It's faster.
#3

[eluser]vile[/eluser]
or you can insert it after upload.


Code:
$config['upload_path'] = /path/to/file;
$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())
{

$data = array(
    'name' => $_FILES['userfile'][name],
    'type' => $_FILES['userfile'][type]
);

$this->db->insert('images', $data);
}else{
  echo $this->upload->display_errors('<p>', '</p>');    
}




Theme © iAndrew 2016 - Forum software by © MyBB