Fenhopi Posted June 14, 2010 Share Posted June 14, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/204688-problems-viewing-an-image-retrieved-from-mysql-database/ Share on other sites More sharing options...
JAY6390 Posted June 14, 2010 Share Posted June 14, 2010 This usually happens when the Content-Type isn't set correctly. You will need to check that is the correct value (image/jpeg for example); Quote Link to comment https://forums.phpfreaks.com/topic/204688-problems-viewing-an-image-retrieved-from-mysql-database/#findComment-1071646 Share on other sites More sharing options...
Fenhopi Posted June 14, 2010 Author Share Posted June 14, 2010 I have this in the upload file: if(!( $type=='image/jpeg' || $type=='image/png' || $type=='image/gif' )) { echo $type . " is not an acceptable format."; echo "<a href=\"form.html\">Click here</a> to try again."; die(); } Quote Link to comment https://forums.phpfreaks.com/topic/204688-problems-viewing-an-image-retrieved-from-mysql-database/#findComment-1071652 Share on other sites More sharing options...
Rustywolf Posted June 14, 2010 Share Posted June 14, 2010 add header('Content-type: image/' . $filetype); Althought my syntax may be wrong so i suggest you look it up Quote Link to comment https://forums.phpfreaks.com/topic/204688-problems-viewing-an-image-retrieved-from-mysql-database/#findComment-1071656 Share on other sites More sharing options...
Fenhopi Posted June 14, 2010 Author Share Posted June 14, 2010 I'm not sure exactly where I'd add that, but I tried adding it in both the upload.php script and the preview picture script and no change. Thanks for the suggestion though! Quote Link to comment https://forums.phpfreaks.com/topic/204688-problems-viewing-an-image-retrieved-from-mysql-database/#findComment-1071673 Share on other sites More sharing options...
PFMaBiSmAd Posted June 14, 2010 Share Posted June 14, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/204688-problems-viewing-an-image-retrieved-from-mysql-database/#findComment-1071674 Share on other sites More sharing options...
Fenhopi Posted June 14, 2010 Author Share Posted June 14, 2010 Lol, it seemed like for some reason it didn't want to display it in Safari. Just firefox. Thanks for the help guys. Quote Link to comment https://forums.phpfreaks.com/topic/204688-problems-viewing-an-image-retrieved-from-mysql-database/#findComment-1071728 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.