How to display image from database in CodeIgniter?

会有一股神秘感。 提交于 2019-12-04 04:57:20

In your database, if i have understood correctly, you're storing the image as C:/wamp/www/my_project/uploads/_1_.jpg

So when you're echoing out the image path the img src attribute, you will have

which won't work as this as local path on your machine. I won't have that image on my file system. The image needs to be accessible on the webserver. (like your index.php file)

So you need the store the image as either this:

uploads/_1_.jpg

and then do <img src="<?php echo $data['screenshot'];?>" />

Or store the image as:

_1_.jpg and and then do

<img src="<?php echo sprintf("uploads/%s", $data['screenshot']);?>" />

EDIT: To be clear: Where you're storing it is correct. But, you don't need the full path in the DB, you just need the web server path.

Controller:

function displayimage($Id=FALSE){
if ($Id)) 
{
    $image = $this->MMarches->getImage($Id);
    header("Content-type: image/jpeg");
    print($image);
}        }

Model:

function getImage($Id){
$data = '';
$Q = $this->db->query("SELECT photo FROM tableWHERE phptoID=".$Id);
if ($Q->num_rows())
{
    $data = $Q->row_array();
    $data = $data['MA_PHOTO']
    $Q->free_result();  
}
return $data;} 

Your View:

src="<?php echo site_url("controller_name/display_image/$image_id"); ?>" 

ALTERNATIVE MODEL:

function getImage($Id){
$Q = $this->db->query("SELECT photo FROM tableWHERE phptoID=".$Id);
   if ($Q->num_rows())       {
           $data = $Q->row_array();
           $data = $data['MA_PHOTO'];
           $Q->free_result();  
   }    
   $size = $data->size();        
   $ret = $data->read($size);     
   return (isset($ret)) ? $ret : '';
 }

For displaying images in web browser you have to give URL of that image rather than PATH of the image.

Following code does not display any image

<img src="C:/wamp/www/my_project/uploads/_1_.jpg"/> 

Following code used URL of the image,Here localhost server name is given, you have to replace your server address with localhost.

<img src="http://localhost/www/my_project/uploads/_1_.jpg"/> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!