0

Im new at CodeIgniter and I want to read some data from mysql database using CodeIgniter. Data I have stored is an image. I have this folder structure in my aplication: 1.ci

  • application

    • controllers
    • views
    • models
    • upload_images (Here I have the images stored as jpg files) When I create this code the image shows up and it is Ok:

  echo ' <img src=" http://localhost/ci/'.$row->book_img.' " />'

While when I try to output images from the database in another way like below it outputs the link instead of the image. I want to use html helper and I have loaded it.

echo '   img(' http://localhost/ci/'.$row->book_img.')';

the image doesn't appear. Do I do something wrong in the second way? Thanks!

Daniel
  • 41
  • 1
  • 8

3 Answers3

0

Try calling it like:

echo img('http://localhost/ci/'.$row->book_img);

Source

You have syntax errors in your second code snippet and you probably don't have error reporting enabled (which is a bad idea during development). Check here for help with setting that up.

Panda
  • 6,955
  • 6
  • 40
  • 55
segFault
  • 3,887
  • 1
  • 19
  • 31
0

The uploads folder should be out side of application folder because the htaccess in the application folder blocks access I also would auto load the url helper

config > autoload.php

$autoload['helper'] = array('url');

Folder Structure For Upload Images

application

system

upload_images <-- folder permission 0777

index.php

then make sure you are getting the image correct on controller var_dump($row->book_img)

Option : 1

$data['img'] = $row->book_img;

$this->load->view('your_view', $data);

View

<img src="<?php echo base_url('upload_images/' . $img);?>" />

Option: 2

$this->load->helper('html');

$data['img'] = img('upload_images/' . $row->book_img); ;

$this->load->view('your_view', $data);

View

<?php echo $img;?>

And set your base url

$config['base_url'] = 'http://localhost/yourproject/';
0

It all depends on what what $row->book_img is returning. If it is only returning the image name, you must first make sure that is has the extension at the end whether it be name.jpg or name.png, etc.

if it is correct you simply use:

$img_src = base_url () . '/PATH_TO_IMAGE_FOLDER/' . $row->book_img;
echo "<img src='$img_src'/>";

If it is returning the full path then:

$img_src = base_url () . $row->book_img;
echo "<img src='$img_src'/>";
CAllen
  • 856
  • 5
  • 14