MGB C February 2016

display image from database in codeigniter

Good day! I am trying to retrieve from image path with the name stored from database but I am struggling to display that image from folder and I'll explain you that later... here is my code.

in my controller

public function save()
    $url = $this->do_upload();
    $title = $_POST["title"];
    $this->main_m->save($title, $url);

public function do_upload()
    $type = explode('.', $_FILES["pic"] ["name"]);
    $type = $type[count($type)-1];
    $url = "./images/".uniqid(rand()).'.'.$type;
    if(in_array($type, array("jpg","jpeg","gif","png")))
        if (move_uploaded_file($_FILES["pic"]["tmp_name"], $url))
    return $url;

in my view im trying to retrieve like this.

 <?php foreach ($this->b->getalldata() as $row) {
      echo '<li>'.
                '<a class="ns-img" href="'.base_url("images/".$row->image).'".></a>'.
                '<div class="caption">'.$row->title.'</div>'.
              } ?>

in my model

public function save($title, $url)
    $this->db->set('title', $title);
    $this->db->set('image', $url);

the image stored to path folder and also the new name of the image stored to database but displaying the image like that is not working for me. my image path folder is "images" and the image name stored to database is "./images/new_name.jpg" not the same name as image that stored to the folder path. the image name that stored to folder path has no ./images/ only new_name.jpg...

how to display that image? someone tried that? help!


Muhammad Azam February 2016

You have error here .base_url("images/".$row->image).'n try to use site_url('images').'/'.$row->image i hope you will get the proper path of your file.

swaroop suthar February 2016

we used codeigniter upload library

         $config = array(
                        'upload_path' => APPPATH."upload/folder",
                        'allowed_types' => "gif|jpg|png|jpeg",
                        'overwrite' => TRUE,
                        'max_size' => "2048000"

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

Punit kumar Tripathi February 2016

You need to explode image name from "./images/new_name.jpg".

$image_arr = explode("/", "./images/new_name.jpg");

echo $image_name = end($image_arr);

Tpojka February 2016

You need img HTML tag:

<img src="<?php echo base_url('images/' . $row->image);?>">

Arvind Jaiswal February 2016

Try this

On view

 <?php foreach ($this->b->getalldata() as $row) {
     echo '<li>'.
       '<a class="ns-img" href="#">'.base_url().'images/'.$row->image.'</a>'.
            '<div class="caption">'.$row->title.'</div>'.
   } ?>     

Post Status

Asked in February 2016
Viewed 2,712 times
Voted 6
Answered 5 times


Leave an answer