Hyaku_ Posted January 9, 2007 Share Posted January 9, 2007 Hi!I'm trying to figure out, how to display image from mysql database, but Firefox says that image contains errors.this is the image forms action page, When I check into database, image is actualy saved there:[code]$img = $_FILES['img']['tmp_name'];$img_size = filesize($img);$img_data = addslashes(fread(fopen($img, "r"), $img_size)); $query = "UPDATE USER_ACCOUNT SET IMG = '$img_data' WHERE ID = 1";$query_exec = mysql_query($query);[/code]img.php:[code]$query = "SELECT IMG FROM USER_ACCOUNT WHERE ID = 1";$query_exec = mysql_query($query);list($img) = mysql_fetch_array($query_exec);Header("Content-Type: image/jpeg");echo stripslashes($img);[/code]index.php:[code]echo "<img src='img.php'>";[/code]but I get 'The image contains errors..' message. Any suggestions? Thank you! Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted January 9, 2007 Share Posted January 9, 2007 echo what is stored in the database for the image..you may have a coding error causing the wrong value to be stored Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 He is echoing it. In order to actually see it as the text instead of an image, you'll need to include the page img.php using include or require in addition to doing the img src. This will help you debug. Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted January 9, 2007 Share Posted January 9, 2007 [quote author=jesirose link=topic=121602.msg500405#msg500405 date=1168318279]He is echoing it. In order to actually see it as the text instead of an image, you'll need to include the page img.php using include or require in addition to doing the img src. This will help you debug.[/quote]A.) I meant echo just the database value not within the img src tags so u can see the actual values stored, guess i should have specified thatand B.) try putting this for your $img = on the page that u upload the pics from[code]$img = basename($_FILES['img']['tmp_name']);[/code] Quote Link to comment Share on other sites More sharing options...
Hyaku_ Posted January 9, 2007 Author Share Posted January 9, 2007 Thanks!I added basename, and here are contents of the file in database:EDIT: Sorry, I mean hear are just a fiew chars, theres tons of random characters, but for some reason copy/paste doesn't quite work for me here..[code]ÿØÿà JFIF..[/code]It is stored into database and that looks like JPEG header, I try to upload gif and change the header(.. image/gif); but still the same thing.. ??? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 That's it? That's not enough to be an image, is it? Quote Link to comment Share on other sites More sharing options...
Hyaku_ Posted January 9, 2007 Author Share Posted January 9, 2007 Sorry, check my edited post! ;)I uploaded gif, here are first fiew chars:[code]GIF87ae÷[/code] Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 Sorry, can't help ya. I normally store images in the filesystem instead of in db, so this is outta my realm. Good luck. Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted January 9, 2007 Share Posted January 9, 2007 Lemme give u my working code real quick:[code]<?php$file_upload = "true";if ($imgfile_size > 125999) {$msg="Your uploaded file must be less than 125kb.<BR>";$file_upload="false";}if (!($imgfile_type=="image/jpeg" || $imgfile_type=="image/gif")){$msg=$msg."Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";$file_upload="false";} if($file_upload=="true") {$uploaddir = 'thumbs/';$uploadfile = $uploaddir . basename($_FILES['imgfile']['name']);echo '<pre>';if (move_uploaded_file($_FILES['imgfile']['tmp_name'], $uploadfile)) {$img_size = filesize($imgfile);$img_data = addslashes(fread(fopen($imgfile, "r"), $img_size)); $query = "UPDATE USER_ACCOUNT SET IMG = '$img_data' WHERE ID = 1";$query_exec = mysql_query($query); echo "File upload was successful.<br><br><a href=\"$uploadfile\">View: $uploadfile</a>\n";} else { echo "File upload failed.\n";}}if($file_upload=="false") {echo" <h2>Error:</h2><br><br>$msg";}?>[/code]oops, i forgot to modify that to work with ur database, one moment please Quote Link to comment Share on other sites More sharing options...
Hyaku_ Posted January 9, 2007 Author Share Posted January 9, 2007 Thanks, but I want to go the MySQL way. :-X Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted January 9, 2007 Share Posted January 9, 2007 The code I posted above should give you a good start..may be alittle buggy cuz I haven't tested it like that but it should put it in your database...you may need to rename some variables and such though cuz mine are different than yours...Oh also I have it set to limit file size to 125k I think..u can easily change that. Quote Link to comment Share on other sites More sharing options...
Hyaku_ Posted January 9, 2007 Author Share Posted January 9, 2007 Thanks man! Could that be posible if I use function addslashes() when inserting data into mysql and then reading the information and using stripslashes(); these two functions lose some special characters like [b]"[/b], [b]'[/b] etc.. from image? Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted January 9, 2007 Share Posted January 9, 2007 The purpose of addlsahes is to escape characters like ' and " making them \' and \" so that the database can take them in in the query without error. stripslashes should in turn take the \ back out and make them ' and " again if those characters are in your files...so in theory when you pull the file back out of the database and stripslashes before calling it in the img src tag it should work fine...although i would test echoing the data with and without stripslashes without the img src tag if you have any errors in the img tags Quote Link to comment Share on other sites More sharing options...
Hyaku_ Posted January 9, 2007 Author Share Posted January 9, 2007 Thanks!The problem was with:[code]echo stripslashes($img);[/code]it works without stripslashes! Quote Link to comment Share on other sites More sharing options...
magic2goodil Posted January 9, 2007 Share Posted January 9, 2007 Sweet deal, glad I could be of assistance :) Quote Link to comment 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.