7

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?

Shabib
  • 1,697
  • 4
  • 20
  • 39
  • Why are you storing the full path like `C:/wamp/www/my_project/uploads/_1_.jpg`? That won't work. You need to refer to the path on the webserver, not the file system. – Flukey Feb 07 '12 at 14:06
  • i am using $image_path = realpath(APPPATH . '../uploads'); to get the real path in the database. if it is not the standard procedure, would you please provide it? – Shabib Feb 07 '12 at 14:13

3 Answers3

3

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.

Flukey
  • 6,445
  • 3
  • 46
  • 71
2

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 : '';
 }
groovekiller
  • 1,122
  • 2
  • 8
  • 20
  • So,in Your vew file, you refer your controller , method and image id..., the echo site_url is used to display images. In your controller, you need to create a method to to print the image. This method has to call image model to get the image url by a sql query – groovekiller Feb 07 '12 at 14:03
  • 1
    Why are you doing `header("Content-type: image/jpeg");` this? he's not storing the image in the DB. He's storing the image name. The image is stored on the file system. And in your `getImage` function, if the resource doesn't exist, then return `false` not `''` – Flukey Feb 07 '12 at 14:04
  • @Flukey can you provide a solution? – Shabib Feb 07 '12 at 14:07
1

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"/> 
shihabudheen
  • 303
  • 1
  • 5
  • 11