I am having a problem displaying a binary blob. My code is below:
<?php
global $Id;
if(!is_numeric($Id))
die("Invalid Id specified");
// Database connection variables
$dbServer = "localhost";
$dbDatabase = "xxx";
$dbUser = "xxx";
$dbPass = "xxx";
$sConn = mysql_connect($dbServer, $dbUser, $dbPass)
or die("Couldn't connect to database server");
$dConn = mysql_select_db($dbDatabase, $sConn)
or die("Couldn't connect to database $dbDatabase");
$dbQuery = "SELECT medium_photo ";
$dbQuery .= "FROM residential ";
$dbQuery .= "WHERE Id = $Id";
$result = mysql_query($dbQuery) or die("Couldn't get file list");
if(mysql_num_rows($result) == 1)
{
$fileType = @mysql_result($result, 0, "image/jpeg");
$fileContent = @mysql_result($result, 0, "medium_photo");
header("Content-type: $fileType");
echo $fileContent;
}
else
{
echo "Record doesn't exist.";
}
?>
Basically, binary code displays in the browser. If I try to look at the source, all I get is the binary code. A co-worker of mine seems to think the following:
You need another transformation before you would have an image. What you have
(I think) is the hex representation of the blob, but not the actual blob. Each
pair of characters occupies 16 bits, but represnts 8 bits of blob data. You need to
run a transformation from the hex encoding to the actual bits. Hex encoding is
used because the hex characters (0-f) are all easy to print and display.
It could be that you have an extra step or a mis-configured step in your processing
that you wound up with hex encoded data.
I do not know how to troubleshoot this. Can you offer any advise or see any issues in the code?