-1

I've tried several times but I have not found a solution, I need to display an image that was saved as BLOB in the database.

Part of the code that saves to the database and displays the data

function manage_report($param1 = '', $param2 = '', $param3 = '')
{
    if ($this->session->userdata('doctor_login') != 1)
        redirect(base_url() . 'index.php?login', 'refresh');

    if ($param1 == 'create') {

        $data['img_principal'] = $this->input->post('img_principal');

        $this->db->insert('report', $data);
        $this->session->set_flashdata('flash_message', get_phrase('report_created'));
        redirect(base_url() . 'index.php?doctor/manage_report', 'refresh');
    }
    if ($param1 == 'edit' && $param2 == 'do_update') {

        $data['img_principal'] = $this->input->post('img_principal');

        $this->db->where('report_id', $param3);
        $this->db->update('report', $data);
        $this->session->set_flashdata('flash_message', get_phrase('account_updated'));
        redirect(base_url() . 'index.php?doctor/manage_report', 'refresh');

    } else if ($param1 == 'edit') {
        $page_data['edit_profile'] = $this->db->get_where('report', array(
            'report_id' => $param2
        ))->result_array();
    } 

    $page_data['page_name']  = 'manage_report';
    $page_data['page_title'] = get_phrase('manage_report');
    $page_data['reports']    = $this->db->get('report')->result_array();
    $this->load->view('index', $page_data);
}

As I'm trying to show on the .html page

<img src="<?php echo $row['img_principal']; ?>"/>

database

CREATE TABLE `report` ( `report_id` int(11) NOT NULL, `img_principal` blo NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Lorena
  • 101
  • 2
  • Possible duplicate of [How to display image from database using php](https://stackoverflow.com/questions/23842268/how-to-display-image-from-database-using-php) – parsa Feb 05 '18 at 11:37
  • Possible duplicate of [How to retrieve images from MySQL database and display in an html tag](https://stackoverflow.com/questions/7793009/how-to-retrieve-images-from-mysql-database-and-display-in-an-html-tag) – simon Feb 05 '18 at 11:40

2 Answers2

0

You need a script which will serve those blobs, call it img.php for instance. One need to pass some kind of ID so it can locate the row in the database as well.

The script can be invoked in HTML:

<img src="/img.php?image_id=1" alt="Database image no. 1">

This script should:

  1. Connect to the database
  2. Locate the data using the (int) $_GET['image_id'] input in this case
  3. Send the Content-Type: image/jpeg header (or appropriate)
  4. Send the Content-Length header (strlen of the blob)
  5. Send the Content-Disposition: inline header
  6. Echo the blob
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
0

Try this:

echo '<img src="data:image/jpeg;base64,'.base64_encode(  $row['img_principal'];).'"/>';
newman
  • 424
  • 2
  • 12