How to display image from database in CodeIgniter?

I am using CodeIgniter 2.1.0 and the MySQL database. I uploaded the image through the form and successfully saved it in the uploads directory, and I also successfully saved the full path to the image in my database. but I had a problem displaying the image, calling the full path from my database.

Here is my download code:

$image_path = realpath(APPPATH . '../uploads'); $config = array( 'allowed_types' => 'jpeg|png|gif|jpg', 'upload_path' => $image_path, 'max_size' => 2097152, 'overwrite' => TRUE, 'file_name' => '_' . $i . '_' ); $this -> load -> library('upload', $config); 

When I save the full path to the image in my database, it looks something like this.

 C:/wamp/www/my_project/uploads/_1_.jpg 

If i try

 <img src="<?php echo $data['screenshot'];?>" /> //($data['screenshot'] refers to the image location retrieved from database) 

it is in my view file, the image is not displayed. What am I doing wrong? Please tell me someone. What is the standard procedure?

+7
source share
3 answers

In your database, if I understand correctly, you save the image as C:/wamp/www/my_project/uploads/_1_.jpg

So, when you echo the image path to the img src attribute, you will have

which will not work as a local path on your computer. I will not have this image on my file system. The image must be available on the web server. (e.g. your index.php file)

So you need to save the image like this:

uploads/_1_.jpg

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

Or save the image as:

_1_.jpg and then do

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

EDIT: Be clear: where do you store it correctly. But you do not need the full path to the database, you just need the path to the web server.

+3
source

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 : ''; } 
+2
source

To display images in a web browser, you must specify the URL of this image, not the PATH of the image.

The following code does not display an image

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

The following code uses the image URL. The name of the local server is indicated here, you must replace your server address with localhost.

 <img src="http://localhost/www/my_project/uploads/_1_.jpg"/> 
+1
source

All Articles