Wayniac Posted January 1, 2010 Share Posted January 1, 2010 I realize its not the best thing to do, but for the sake of learning I am uploading an image to a MySql database, which works fine. But when I try and retrieve it, many lines of 3Éí5æ/Hÿ�M‘þs?ô’_ó¤¦Èÿ�9ŸúIláà7 stuff appears. Not sure if its a quick fix or not, should you need the code, just let me know. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/ Share on other sites More sharing options...
.josh Posted January 1, 2010 Share Posted January 1, 2010 how are you uploading it, what column type are you storing it in, and how are you outputting it? Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986611 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 Thank you so much for your reply! Below is how I am uploading it: (Did you require the HTML?) <?php //error_reporting(0); include("config.php"); // Connect to database if ($_REQUEST[completed] == 1) { $newname = uniqid("whc").".jpg"; move_uploaded_file($_FILES['imagefile']['tmp_name'], "../upload/$newname"); } if(isset($_POST['submit'])) { $newname = mysql_real_escape_string(file_get_contents("../upload/$newname")); //$newname = mysql_real_escape_string($_POST['imgdata']); $title = mysql_real_escape_string($_POST['title']); $age = mysql_real_escape_string($_POST['age']); $testimonial = mysql_real_escape_string($_POST['testimonial']); if(!$title){ echo "Error: Entry title is a required field."; exit(); } $result = mysql_query("INSERT INTO album (imgdata, title, dtime, age, testimonial) VALUES ('$newname','$title',NOW(),'$age','$testimonial')",$connect); echo "<b>Entry UPDATED Successfully!<br>You'll be redirected to View Album page after<br>(4) Seconds"; echo "<meta http-equiv=Refresh content=4;url=index.php>"; } else { ?> <?php include("config.php"); $image = stripslashes($_REQUEST[image]); $result = mysql_query("select * from album where albumid=\"". addslashes($image).".jpg\""); $myrow = mysql_fetch_assoc($result); $imagebytes = $myrow[imgdata]; header("Content-type: image/jpeg"); print $imagebytes; ?> Here is where the image is being displayed, or at least should be, all that is being displayed here is all that gibberish and the text entries which work fine. <?php include("config.php"); $albumid = $_GET['albumid']; $result = mysql_query("SELECT * FROM album WHERE albumid='$albumid' ",$connect); while($myrow = mysql_fetch_assoc($result)) { echo "<b>Title: </b>"; echo $myrow['title']; echo "<b><br>Posted: </b><i>"; echo $myrow['dtime']; echo "</i><b><br>Age (Years):</b>: "; echo $myrow['age']; echo "<br><br><a href=\"javascript:self.history.back();\"><-- Go Back</a>"; echo "<b><br><br><br><br>Image: <br><br></b>"; echo "<img src=get_image.php?image={$myrow['imgdata']}>"; } ?> Once again, thank you for offering your assistance. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986613 Share on other sites More sharing options...
premiso Posted January 1, 2010 Share Posted January 1, 2010 What is the column type of the MySQL column you are uploading it to? Is it blob, if not it should be. This SQL statement is bad: $result = mysql_query("select * from album where albumid=\"". addslashes($image).".jpg\""); It should be: $result = mysql_query("select * from album where albumid='". addslashes($image).".jpg'"); As string values inside of MySQL need to be encapsulated in Single quotes only to be valid syntax. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986631 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 Made that change, no effect. Its a bloblong. Tried blob aswell and same thing. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986635 Share on other sites More sharing options...
premiso Posted January 1, 2010 Share Posted January 1, 2010 Well the text you are getting is the correct text as that is the binary code of the image. Looking at the code the only thing I can think of is there is some reason the content-type is not taking so the browser does not know it is suppose to display it as an image instead of the binary code. Perhaps try not using mysql_real_escape_string on the image when you input it into the database and see if that makes any difference? Other than that I am not sure. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986638 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 I believe I may have found why, but keep getting a parse error. I changed: echo "<img src=get_image.php?image={$myrow['imgdata']}>"; To give them quotes around the src: echo "<img src="get_image.php?image={$myrow['imgdata']}">"; But now this parse error pops up?? Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /mnt/w0712/d16/s01/b02c73a9/www/example.ca/testalbum/read_more.php on line 374 Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986643 Share on other sites More sharing options...
premiso Posted January 1, 2010 Share Posted January 1, 2010 You would need to escape the double quotes: echo "<img src=\"get_image.php?image={$myrow['imgdata']}\">"; But I doubt that is the issue. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986644 Share on other sites More sharing options...
Stephen Posted January 1, 2010 Share Posted January 1, 2010 For uploading image data, try using base64_encode (on the image data before you insert it into the database) and to display it use base64_decode. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986657 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 That corrected the parse error premiso, thank you, but unfortunately your right, it never fixed the problem. Hi Stephen, could you explain to me or give an example on how I might accomplish such? I never heard about base64_encode before... (still fairly new and learning) Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986659 Share on other sites More sharing options...
premiso Posted January 1, 2010 Share Posted January 1, 2010 if(isset($_POST['submit'])) { $newname = base64_encode(file_get_contents("../upload/$newname")); //$newname = mysql_real_escape_string($_POST['imgdata']); For the displaying file: <?php include("config.php"); $image = stripslashes($_REQUEST[image]); $result = mysql_query("select * from album where albumid=\"". addslashes($image).".jpg\""); $myrow = mysql_fetch_assoc($result); $imagebytes = $myrow[imgdata]; header("Content-type: image/jpeg"); print base64_decode($imagebytes); ?> Is what he means. Not sure if it will work, but I do remember something like this is the past workings. Worth a shot. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986660 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 That did something... It made it so that all the gibberish binary content disappeared but the small image icon 24x24 pixels is there by itself now. When I right click on it and view the pictures properties it says unknown size. Really appreciate the help.. REALLY DO! Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986664 Share on other sites More sharing options...
Stephen Posted January 1, 2010 Share Posted January 1, 2010 I was getting confused on your display image script (because I thought it actually took image data through the url, which couldn't have worked), but basically what premiso said is correct. Not sure why it outputs a small icon :\ Can you PM or post some of your data in the album table? Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986665 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 Here is an image of the database structure being used. http://www.lifelikemedia.ca/upload/db_info.jpg Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986667 Share on other sites More sharing options...
Stephen Posted January 1, 2010 Share Posted January 1, 2010 Ah, I meant the actual data itself (like the base64_encrypted data). Right now you could try using this code instead of the old one: (below) Try creating another album and see if you get the same little icon (and check if the information is different in the table for the two different images). EDIT: Actually, after looking at it, this ($result = mysql_query("select * from album where albumid='".addslashes($image).".jpg'") would not work if your "albumid" is an auto_incrementing integer. Try using these: <?php include("config.php"); $image = stripslashes($_GET['image']); $result = mysql_query("select * from album where albumid='". addslashes($image)."'"); $myrow = mysql_fetch_assoc($result); $imagebytes = $myrow['imgdata']; header("Content-type: image/jpeg"); print base64_decode($imagebytes); ?> (the display script): <?php include("config.php"); $albumid = $_GET['albumid']; $result = mysql_query("SELECT * FROM album WHERE albumid='$albumid' ",$connect); while($myrow = mysql_fetch_assoc($result)) { echo "<b>Title: </b>"; echo $myrow['title']; echo "<b><br>Posted: </b><i>"; echo $myrow['dtime']; echo "</i><b><br>Age (Years):</b>: "; echo $myrow['age']; echo "<br><br><a href=\"javascript:self.history.back();\"><-- Go Back</a>"; echo "<b><br><br><br><br>Image: <br><br></b>"; echo "<img src=\"get_image.php?image={$myrow['albumid']}\">"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986669 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 Okay so I tried that and the second set has no data for the image. The other entries still work fine, but 0 bytes comes up in the database under the imgdata field. This only happened when I added the base64_decode / encode. Before this, it was uploading data, but giving me that binary mumbo jumbo... Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986670 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 How do you say I love you in EVERY different language??? That worked!!! You can only imagine how I feel now... I scared my dog Ollie by screaming the loudest YES ever. Thank you all, is there any information I can post to help future viewers? Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986671 Share on other sites More sharing options...
Stephen Posted January 1, 2010 Share Posted January 1, 2010 Can't think of anything else, but I recommend you check your personal messages. Also, there is a "Solved" button somewhere around your topic you can press xD. I think it's at the very bottom, but I haven't posted any topics too recently so I don't remember. Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986672 Share on other sites More sharing options...
Wayniac Posted January 1, 2010 Author Share Posted January 1, 2010 Will do, and once again, thank you Quote Link to comment https://forums.phpfreaks.com/topic/186827-php-upload-image-turns-into-gibberish/#findComment-986673 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.