View Single Post
Old 04-04-2005, 02:19 PM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Using PHP to retrieve images stored in MySQL

I'm storing a few images in my database (yes, I know both sides of the debate, and I know I can just store the images' URLs in the database and pull them that way. This is an experiement. I'm doing it this way if it's at all possible. Period.)

Can you tell I've had problems getting this question answer in other forums?

Anyhoo, I've got a script that uploads the images and file-type information:
PHP Code:
<?
$db 
mysql_connect("mysql_location""username","password");
mysql_select_db("database",$db); 

if (isset(
$binary_File) && $binary_File != "none"
{
$fp=fopen($preimgdata,'rb');
$binary_File_type=exif_imagetype($preimgdata);
$midimgdata=fread($fp,filesize($preimgdata));
$data addslashes($midimgdata);
fclose($fp);
  
$sql "INSERT INTO images_table (imgdata, imgtype) VALUES ('$data', '$binary_File_type')";
    
  
$result mysql_query($sql$db);
    
 echo 
"<font face=verdana size=2>The file was successfully added to our database.<P>";
}
mysql_close();
?>
which seems to work fine (I can see the data and type in the using a mySQL frontend)

and I'm trying to pull them back down using this file, called pulldown.php and using the convention pulldown.php?id=idnumber:

PHP Code:
<?php
$db 
mysql_connect("mysql_location""username","password");
mysql_select_db("themid",$db); 
$sql "SELECT imgdata, imgtype FROM filmat11 WHERE id=".$id;
  
$result = @mysql_query($sql$db);
  
$data = @mysql_result($result0"imgdata");
  
$type = @mysql_result($result0"imgtype");
  
header("Content-type: $type");
  echo 
$data;
?>
The problem is, this produces, repeatedly, a broken image. Any ideas?
metazai is offline   Reply With Quote