patfawb Posted March 25, 2009 Share Posted March 25, 2009 I'm getting a bizarre error while trying to get an image from my database. I think it has something to do with my file: getPhoto2.php Have a look at my PHP code so far. The result of running the code is that it prints the imageurl as an image, but of the url, not the ACTUAL image. Strange i know. /// ---------- Upload.php --------- /// <html> <head> <title>Picture Upload</title> </head> <body> <h1>Please Work</h1> <p>Enter the picture you wish to upload the the picture ID</p> <form action=<?php print $PHP_SELF; ?> method="post" enctype="multipart/form-data"> Picture to load: <input type="file" name="imageFile" size="50" /> <br/>Views <input type="text" name="count" size="4"/> <br/><input type="submit" value='Upload'> </form> </body> </html> <?php // print_r($_REQUEST); // print_r($_FILES); // should check that this is a valid count if (!isset($_REQUEST['count'])) die(""); $count = $_REQUEST['count']; // details of the file stored on the server are in the $_FILES array $filename = $_FILES['imageFile']['name']; $tempfilename = $_FILES['imageFile']['tmp_name']; // get rid of any newline characters in the file $tempfile = file($tempfilename); $imageJPEG=implode($tempfile,''); //base64 is one possible encoding of a binary file into printable characters - it takes more space //and takes time to code and decode but it can be manipulated as a string //an alternative would be to save as binary but I have difficulty making that work reliably :-( $image64 = base64_encode($imageJPEG); // connect to the MySQL server include("wdllib.php"); $dblink =db_connect(); //replace is used here instead of insert to insert or replace $query = "replace into picture(views,imageurl) values($count,'$image64')"; if(!$dbresult = mysql_query($query,$dblink) ) { print "Query failed :"; print(mysql_error() . "<br/>"); exit; } $pictureid = mysql_insert_id(); // close the database print("<p>$filename added, size = ". strlen($image64)."</p>"); print("<img src=\"getPhoto2.php?pictureid=$pictureid\" />"); mysql_close($dblink); ?> /// ------------- getPhoto2.php ------------- /// <?php /* retrieve the stored photo for an employee input pictureid */ include ("wdllib.php"); $dblink = db_connect(); $pictureid = $_REQUEST['pictureid']; $query = "select * from picture where pictureid=$pictureid;"; if(!$dbresult = mysql_query($query) ) { print "Query failed :"; print(mysql_error() . "<br/>"); exit; } if ($row = mysql_fetch_object($dbresult)) { // set the content type to image/jpeg - in fact a browser will also look at the image file itself to detect what it is header("Content-type: image/jpeg"); // decode back from base64 $imagejpeg= base64_decode($row->image); print $imagejpeg; } else "$pictureid has no image"; ?> /// ------------------------- /// The properties in my database are as follows: pictureid imageurl views any help would be greatly appreciated. Cheers guys. Link to comment https://forums.phpfreaks.com/topic/151137-getting-image-from-database/ Share on other sites More sharing options...
br0ken Posted March 25, 2009 Share Posted March 25, 2009 Unfortunately I can't see what your error is but I found this cool script yesterday that gets photo's. It's quite good as it can resize images on the fly. Here's the link if you want to check it out http://shiftingpixel.com/2008/03/03/smart-image-resizer/ Link to comment https://forums.phpfreaks.com/topic/151137-getting-image-from-database/#findComment-794041 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.