Hello. I have successfully written a script that allows me to store an image in a MYSQL database. However, I'm now trying to alter the script to work with a MSSQL database. The image seems to get placed into the database OK, but when I try to display it, I get an error message saying
The image could not be displayed because it contains errors
Can someone look at this and offer some suggestions? Like I said, it works for MYSQL. The only difference in the tables is that in MYSQL the image is stored in a field type of BLOB (binary), and in MSSQL, I have the field set to IMAGE (binary).
Code that inserts the image:
if(isset($_POST['submit']))
{
if ($_FILES['userfile']['error'] == 0)
{
$fn = $_FILES['userfile']['tmp_name'];
$max_img_width = 200;
$img_orig_size = getimagesize($_FILES['userfile']['tmp_name']);
$img_orig_width = $img_orig_size[0];
$img_orig_height = $img_orig_size[1];
$mlt = $max_img_width / $img_orig_width;
$img_new_width = $max_img_width;
$img_new_height = round($img_orig_height * $mlt);
$img_resized = imagecreatetruecolor($img_new_width, $img_new_height);
imagecopyresampled($img_resized, imagecreatefromjpeg($_FILES['userfile']['tmp_name']), 0,0,0,0, $img_new_width, $img_new_height, $img_orig_width, $img_orig_height);
imagejpeg ($img_resized, $fn);
$size = filesize($fn);
$type = $_FILES['userfile']['type'];
$fp = fopen($fn, "r");
$contents = ereg_replace("'","''",(fread($fp, $size)));
$checksum = md5($contents);
$query = "INSERT INTO pressboxImage (md5, imgsize, image, type) VALUES ('$checksum', '$size', '$contents', '$type')";
//echo $query;
$result = mssql_query($query) or die();
imagedestroy($img_resized);
fclose($fp);
}
}
?>
<form enctype="multipart/form-data" action="<? echo $PHP_SELF; ?>" method="post">
ID:
<input type="textfield" name="id">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
Send this file: <input name="userfile" type="file">
<input name="submit" type="submit" id="submit" value="Send File">
</form>
Code to display image:
$checksum = $_GET['imgId'];
$query = "SELECT image FROM pressboxImage WHERE imageId = 1";
$result = mssql_query($query) or die();
$row = mssql_fetch_array($result);
header("Content-type: ".$row['type']);
print $row['image'];
I know you don't necessarily need to stuff the images into a database, you could just store them in a directory. But, I'd like to see if this works just for my own satisfaction. Whether or not I use it is a different story.