简体   繁体   中英

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(is_uploaded_file($_FILES["pic"]["tmp_name"]))
        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>'.
          '</li>';
              } ?>

in my model

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

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!

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

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

echo $image_name = end($image_arr);

we used codeigniter upload library

 $this->load->library('image_lib');
         $config = array(
                        'upload_path' => APPPATH."upload/folder",
                        'allowed_types' => "gif|jpg|png|jpeg",
                        'overwrite' => TRUE,
                        'max_size' => "2048000"
                    );


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

您需要img HTML标签:

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

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>'.
      '</li>';
   } ?>     

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM