Hi all,
I am trying to store an image on a SQL Server table and then retrieve it and display it on a web page. I have used in the past the URL approach (saving the image in a folder and linking to it) but now want to store these images in the database (as a means to expand my PHP knowledge really).
I have searched the web and the forum and I found explanations as to how to do this using MySQL. I adapted this thinking it would be straight forward but it doesn't seem to work (not sure why), hence this thread.
Here is what I do:
1) I have a table in my database with a column to store the Image. I have tried different data types in case this was the problem, but I haven't been able to make it work with either IMAGE, NTEXT or TEXT.
2) I use a form to upload the image file and store it in the database (I've basically adapted the code on this page: http://codewalkers.com/tutorials/35/3.html ), only thing to mention is that I needed to use strreplace when inserting the image to replace ' with '' (double single), as otherwise single quote characters confuse SQL Server.
3) To display the image I have a separate PHP file to which I pass the image ID as a GET argument like this:
echo "<IMG SRC='Render_DBImage.php?ImageID=". $ImageID ."'>";
4) This is the file Render_DBImage.php:
<?php
require($_SERVER['DOCUMENT_ROOT'].'/sserver/includes/database.inc');
$ImageID = $_GET["ImageID"];
$sql = "SELECT ImageType, [Image], ImageSize, ImageName FROM [image] WHERE ImageID=". $ImageID;
$process=odbc_exec($sqlconnect, $sql);
odbc_fetch_row($process);
$ImageType = odbc_result($process,"ImageType");
$Image = odbc_result($process,"Image");
Header ("Content-type: $ImageType");
print $Image;
odbc_close($sqlconnect);
?>
But the image is not displayed. In fact, if I try the following on my Firefox web browser:
[url]http://localhostRender_DBImage.php?ImageID=1[/url]
I can't see the image and choosing view page source shows a bunch of binary text (what is stored in the database).
Any ideas why the image is not being rendered? I just don't seem to be able to figure it out!. If you need anymore details, the PHP source code, etc... let me know please. Thanks,
Javier