Mysql – How to display image from database in CodeIgniter

codeigniter-2image-processingimage-uploadingimageviewmysql

I am using CodeIgniter 2.1.0 and MySQL database. I have uploaded an image through a form and successfully stored it in a uploads directory and I have also successfully stored the full path of the image in my database. but i am having problem with showing the image by calling the full path from my database.

Here is my code for the upload:

$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 am storing the full path of 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)

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

Best Solution

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.