Jump to content

Problems viewing an image retrieved from mysql database


Fenhopi

Recommended Posts

Hey, I'm having some problems viewing images the I've uploaded to my mysql database.

 

When I go to the direct link to the image (getpic.php?pid=1) I get a page full of symbols the likes of this:

����JFIF``��C	!"$"$��C����"��	 ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B����	#3R�br� $4�

 

Here is the code I use to retrieve the picture:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<? 
if(isset($_GET['fid'])) 
{ 
// connect to the database 
include "connect.php"; 

// query the server for the picture 
$fid = $_GET['fid']; 
$query = "SELECT * FROM files WHERE fid = '$fid'"; 
$result  = mysql_query($query) or die(mysql_error()); 

// define results into variables 
$name=mysql_result($result,0,"name"); 
$size=mysql_result($result,0,"size"); 
$type=mysql_result($result,0,"type"); 
$content=mysql_result($result,0,"content"); 

// give our picture the proper headers...otherwise our page will be confused 
header("Content-Disposition: attachment; filename=$name"); 
header("Content-length: $size"); 
header("Content-type: $type"); 
echo $content; 

mysql_close(); 
}else{ 
die("No file ID given..."); 
} 

?>
<img src="getpicture.php?fid=1">
</body>
</html>

 

Please help me solve this issue!

Assuming your <img tag is like -

 

<img src="getpicture.php?fid=1" alt="">

 

the php code that you put in getpicture.php must only output the correct header() statements followed by the binary image data. You cannot output anything else (i.e. html) in the getpicture.php file.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.