joebegborg07
joebegborg07

Reputation: 849

php - Unable to display image

I'm very new to development, and I'm trying to retrieve an image from an SQL server over an odbc connection using the below:

<?php

require ('connect.inc.php');



$sql = "SELECt image FROM test.dbo.clients WHERE id = 1";
$sql_exec = odbc_exec($con, $sql);

if($sql_array = odbc_fetch_array($sql_exec)){
    $image = base64_encode($sql_array['image']);
    header('Content-type: image/jpeg');
    echo "<img src=".$image."/>";
}


?>

The issue is that I'm getting an icon showing a broken image.
The query is correct as when I change it to the code below, it returns this string instead of the image:

Q2hyeXNhbnRoZW11bS5qcGc=

<?php
require ('connect.inc.php');   

$sql = "SELECt image FROM test.dbo.clients WHERE id = 1";
$sql_exec = odbc_exec($con, $sql);

if($sql_array = odbc_fetch_array($sql_exec)){
    $image = base64_encode($sql_array['image']);

    echo $image;
}
?>

I know the code is not correct and might be vulenrable to SQL injection, however I'd appreciate if you can help me retrieve the image.

Many thanks in advance, J

Upvotes: 0

Views: 72

Answers (1)

Werner Henze
Werner Henze

Reputation: 16781

If you decode the base64 string you get, then you'll see that the decoded data is Chrysanthemum.jpg. This is just the filename of the image, not the image data.

You need to either store the image in the database (not the filename) or add some code to read the image from the filesystem.

BTW, Content-Type image/jpeg requires that the data is the raw image, but your content (<img src=...> ...</img>) is an HTML fragment.

Upvotes: 1

Related Questions