how to store and retrieve images in database using CodeIgniter
27,679
Solution 1
DO NOT STORE FILES INSIDE THE DATABASE!!!
This is always a bad design idea. Store the files in the file system and simply store the file names and point to the file, it will save you a lot of headaches in the future.
Solution 2
// uploading
public function do_upload(){
...
$image_path=$this->upload->data();
$uploaded_image = $image_path['full_path'];
// Read the file
$fp = fopen($uploaded_image, 'r');
$data = fread($fp, filesize($uploaded_image));
$data = addslashes($data);
fclose($fp);
// here you can easy insert $data to 'a_photo' column.
}
// Viewing, $image_id is row id
public function getImage($image_id){
// select $row from database as usual and then
$content = $row['a_photo'];
echo '<img src="data:image/jpeg;base64,'.base64_encode($content).'">';
}
In your template:
<?php getImage(12); ?>
where 12 is row id.
Author by
brijeshp09
Updated on July 09, 2022Comments
-
brijeshp09 almost 2 years
I am using a WAMP server, and I want to upload images in the database using CI.The image variable in database is of blob datatype. My question's as follows:
1) How to store the image instead of the file name and what datatypes should I use?
2) How to retrieve images from the DB?
My controller's code:
<?php class Image_control extends CI_Controller{ function index() { //$this->load->view('image_view'); //$this->Image_model->do_upload(); $data['images']=$this->Image_model->get_images(); $this->load->view('image_view',$data); } function do_upload() { $config = array( 'allowed_types' => 'jpg|png|bmp', 'upload_path'=>'./images1/', 'max_size'=>2000 ); $this->load->library('upload',$config); if (!$this->upload->do_upload()) { $errors[]=array('error'=>$this->upload->display_errors()); $this->load->view('image_view',$errors); } $image_path=$this->upload->data(); $file_name=$image_path['file_name']; $config = array( 'a_name' => $this->input->post('a_name'), 'a_details'=>$this->input->post('a_info'), 'a_photo'=>$file_name ); $insert=$this->db->insert('animalstore',$config); return $insert; } } ?>
My model's code:
<?php class Image_model extends CI_Model { function get_images() { $query = $this->db->get('animalstore'); if($query->num_rows > 0 ) { foreach($query->result() as $rows) { $data[] = $rows; } return $data; } } } ?>
And finally here's the code for my view:
<?php echo form_open_multipart('image_control/do_upload'); echo form_input('a_name','Animal Name'); echo form_input('a_info','Animal Information'); echo form_upload('userfile'); echo form_submit('upload','Upload'); echo form_close(); ?> <?php foreach ($images as $image):?> <h1><?php echo $image->a_name;?></h1> <h1><?php echo $image->a_details;?></h1> <img src = "/<?php// echo ltrim($image->a_photo, '/'); ?>" > <img src="http://localhost/ci_test/images1/<?php echo $image->a_photo;?>"/> <img src="<?php //echo sprintf("images/%s", $image['screenshot']);?>" /> <h1><?php// echo $image->a_photo;?></h1> <?php endforeach; ?>
I tried solving it in different ways and searched for my problem but I didn't find any appropriate answer.
-
cryptic ツ almost 11 yearsWhy would you call a controller method within a template/view?
-
ToxaBes almost 11 yearsIt's just an example for showing how it's working. In real world getImage() will be called in controller, result assigned to some template variable and then showed in template. And in real world TravisO's answer is fully correct :)
-
Christian Suarez over 10 yearsI don't agree with that, what happens when the server is on a cluster? and what's happen on the backups? for my the best design approach is store the images on a database.
-
Rid Iculous over 10 yearsthat's not storing it in the DB
-
Rid Iculous over 10 years-1 for providing an opinion rather than adding a solution. Portability eg is one of many advantages of storing images (or other files for that matter) in a DB.
-
Rmaxx almost 4 yearsHi,please explain what your solution does and how it differs from the other answers.