CodeIgniter Forums
upload image using model/view/controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: upload image using model/view/controller (/showthread.php?tid=11678)



upload image using model/view/controller - El Forum - 09-19-2008

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


upload image using model/view/controller - El Forum - 09-19-2008

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


upload image using model/view/controller - El Forum - 09-22-2008

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