Jump to content


Photo

Retrieve Images From mySQL


  • Please log in to reply
4 replies to this topic

#1 icE1

icE1
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 September 2006 - 10:49 AM

Hi,

I'm trying to retrieve an image that I stored in a mySQL database. I'm able to get the name, id but however unable to get the image to show up. The image shows up as:

ótvwnY·ÌôMëÉØö,`âJ5œ››úõÐ׫hÚnÑ'«©”Œèè褤£üüüÎÎÎæ¶y‰­ŽØ’æææN…QÔÔÔŠ™¼EÛúúúF¶üÊñ…u’;¹s§øÚÚÚòiVò“…–––q†nkÝÝÞQºººNm±°°°”jvdùèåááá{“Å6«õxf/W®‘¤Í¶¶¶ÚåøÑÑÑd—ø­­­°¸ÈúârÎɹ½½¾êÑjòòòø¸­sÈÈÈööö™‡vïïðMXnÅÅÅ=Âøøø¨†vNHÆ€€©©©ôôô‹SLñõýíí튋

My code for image retrieve is as follow:

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'config.php';
include 'opendb.php';

$query = "SELECT id, name, content FROM upload";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name, $content) = mysql_fetch_array($result))
{
?>
<?php
echo $name;
echo $id;
echo $content;
?>
<?php 
}
}
include 'closedb.php';
?>
</body>
</html>

<?php
if(isset($_GET['id'])) 
{
// if id is set then get the file with the id from database

include 'config.php';
include 'opendb.php'; 
$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) =                                  mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

include 'closedb.php'; 
exit;
}

?>

Can anyone help me? What have I left out?

#2 eipihipl

eipihipl
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 14 September 2006 - 02:08 PM

you've forgotten the content-type HTTP header

header('content-type: image/jpeg');

cheers

#3 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 14 September 2006 - 03:08 PM

If you made it into the second control path, you should be fine, from the looks of it.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#4 icE1

icE1
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 14 September 2006 - 04:28 PM

you've forgotten the content-type HTTP header

header('content-type: image/jpeg');

cheers


Sorry but which part of my code should I insert this code in?

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 14 September 2006 - 05:05 PM

I'm still confused as to why you have the first part of that PHP script there at all.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users