PHP - Retrieving Image From Database (blob)? Just Getting Binary.
Hi,
I have managed to get the code working to store a .jpg file in the database under the longblob type. Now all i have left to do is to retrieve that image and display it. So far i have this: list.php Code: [Select] while($r = mysql_fetch_array($sql)) { //for each record ... echo " <img src= getoutside.php?id='".$r[apartmentId]. " '> "; getoutside.php Code: [Select] <?php header("Content-type: image/jpg"); // act as a jpg file to browser $nId = $_GET['id']; include 'dbase.php'; //connect to database $sqlo = "SELECT outside FROM apartment WHERE apartmentId = $nId"; $oResult = mysql_query($sqlo); $oRow = mysql_fetch_array($oResult); $sJpg = $oRow["outside"]; echo $sJpg; ?> The result from this is a box with a red cross in it. Can anyone find a problem with this code please? Thanks Similar TutorialsI'm planning on storing RIPEMD-160 passphrase hash's raw binary data in MySQL as BINARY(20). Right now, I'm not sure if one of the following INSERT approach is more preferrable and more efficient: Approach (1) - Using mysqli::real_escape_string() ================================================= Code: [Select] $binhash = $mysqli->real_escape_string(hash('ripemd160', $passphrase, true)); $mysqli->query("INSERT INTO testtbl (Passphrase) VALUES ('" . $binhash . "')"); This approach saves hash() from converting raw binary data to hexadecimal format, but need to process and escape the binary string. Approach (2) - Using UNHEX() on MySQL ===================================== Code: [Select] $hexhash = hash('ripemd160', $passphrase); $mysqli->query("INSERT INTO testtbl (Passphrase) values (UNHEX('" . $hexhash . "'))"); This approach needs hash() to convert binary data to hexadecimal but no need for escaping the string and it put extra load on MySQL to convert the hexadecimal back to binary. Hi everyone, i am just trying to learn php for a bit of fun really and started making a sort of 'facebook' website. I am having trouble however trying to display different users images, for example when trying to find a correct 'friend' only the image of the last result is being shown for all people with the same name... here is my code below, if anyone can help me out that would be great file 1 $count=1; while ($numids>=$count){ echo "<form method=\"post\" action=\"friendadded.php\">"; $frienduserid=$_SESSION["passedid[$ii]"]; $friendfirstname=$_SESSION["passedfirstname[$ff]"]; $friendlastname=$_SESSION["passedlastname[$ll]"]; $_SESSION['friendsuserpicid'] = $frienduserid; echo "<table width=\"700\" height=\"50\" border=\"1\" align=\"center\">"; echo "<tr>"; echo "<th></th>"; echo "<th>First Name</th>"; echo "<th>Last Name</th>"; echo "</tr>"; echo "<tr>"; echo "<td><center>"; echo "<img border=\'0\' src=\"frienduserpic.php\" width=\"80\" height=\"80\" align=\"middle\"/>"; echo "</center></td>"; echo "<td><center>"; echo $friendfirstname; echo "</center></td>"; echo "<td><center>"; echo $friendlastname; echo "</center></td>"; echo "</tr>"; echo "</table>"; echo "<center><input type=\"submit\" value=\"Add this friend\" name=\"Add Friend\"></center><br/>"; $ii=$ii+1; $ff=$ff+1; $ll=$ll+1; $count=$count+1; echo "</form>"; } file 2 session_start(); $passeduserid=$_SESSION['friendsuserpicid']; $timespost=$_SESSION['postednum']; $host= $username= $password= $db_name= $tbl_name= mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $query = mysql_query("SELECT * FROM $tbl_name WHERE picid='".$passeduserid."'"); $row = mysql_fetch_array($query); $content = $row['image']; header("Content-type: image/jpeg"); echo $content; Thanks in advance Hi! I am building a little PHP/MySQL application where pictures are uploaded and stored in MySQL in a longblob. These are special circumstances and storing images in the database in an absolute must. Uploading is working fine. The upload script inserts the following into the field `image_data`: base64_encode(file_get_contents($_FILES['image']['tmp_name'])) The problem is displaying the images. I cannot for the life of me make it work. I've tried the code on multiple systems with various versions of PHP and MySQL. I have two files: one called view.php and one called show.php. # view.php # It gets $info['id'] from a query getting the ID of the recent-most image uploaded. echo '<img src="show.php?id='.$info['id'].'" alt="" />'; # show.php # $id is determined by $_GET['id'] and passes through security checks I've omitted here. # There is zero (not even a whitespace) output before the header()s are sent. $query = "SELECT `image_data`,`image_mime`,`image_size` FROM `upload`.`files` WHERE `id` = '$id';"; $sql = mysql_query($query) or die(mysql_error()); $image = mysql_fetch_assoc($sql); header('Content-Type: ' . $image['image_mime']); header('Content-Length: ' . $image['image_size']); echo base64_decode($image['image_data']); The problem is that no image is displayed either in view.php or when I call show.php directly with a valid ID. I have verified that $image['image_mime'] and $image['image_size'] contain the right data. However, if I download show.php and change extension to for example .jpg, the image is there. So the image is stored correctly in the database and $image['image_data'] is outputting the right data. I even compared checksums for the image before and after and they're identical, so I would conclude that the error is in the outputting of the image - but I can't figure out what. Error_report is set to E_ALL but there's nothing useful coming out. Any ideas? Code: [Select] $query = mysql_query("SELECT a.*, b.* FROM friendlist a INNER JOIN friendlist b ON (a.friendemail=b.friendemail) INNER JOIN users c ON (b.friendemail = c.EmailAddress) WHERE a.email = 'asdf@gmail.com' AND c.Username LIKE '%carol%' GROUP BY a.id ORDER BY count(*) DESC"); Code: [Select] while ($showfriends = mysql_fetch_array($query)) { echo $showfriends['Username']; } and I would get nothing. It produces the correct number of <div> so i know it's getting through, but it's having trouble displaying the entries? Hi everyone, I've read lots of tutorials on this, but something is not clicking in my brain with it. I think my coding is close, but, as of right now, all I get is a red x for the image when I try to display the image. Let me share my code as a starting point - I would truly appreciate any helpful comments or blatant errors that are pointed out or shared with me. Here is the upload image form and code (so they clicked the item name and then go into this): if($_REQUEST['modifyfeatured']) { $id=$_REQUEST['modid']; $sqlid="SELECT * FROM product WHERE id='$id'"; $resultid=mysql_query($sqlid, $dbh); $idrow = mysql_fetch_array($resultid); echo "<p>"; echo "Fill out the form below to add a short text line (i.e. 20% Off!) and/or an image (like the new note).<br>"; echo "You are modifying the following item: <p><b>"; echo $idrow['product_id']; echo " "; echo $idrow['title']; echo "</b><p>"; ?> <table border="0" cellpadding="2" cellspacing="0"> <tr> <td> <form method="post" enctype="multipart/form-data"> <input type="hidden" name="id" value="<? echo $id; ?>"> Short Text Message: </td><td> <input type="Text" name="message"></td></tr> <tr><td>Small Image:</td><td> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"></td></tr> <tr><td> </td><td> <input name="upload" type="submit" class="box" id="upload" value="Submit"> </td></tr></table> </form> <? } //then when the click the upload link, here is the code for that: if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $message=$_REQUEST['message']; $id=$_REQUEST['id']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include 'library/config.php'; include 'library/opendb.php'; //$query = "INSERT INTO featured_prods (name, size, type, image, message ) ". //"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$message')"; $query="UPDATE featured_prods SET name='$fileName', size='$fileSize', type='$fileType', image='$content', message='$message' WHERE id='$id'"; //$sqldone="UPDATE product SET product_id='$product_id', title='$title', description='$description', regular_price='$regular_price', sale_price='$sale_price', stat='$stat', weight='$weight', close_out='$close', additional='$additional', additionalpix='$additionalpix_name' WHERE id='$id'"; //$resultdone=mysql_query($sqldone, $dbh); mysql_query($query) or die('Error, query failed'); include 'library/closedb.php'; echo "<br>File $fileName uploaded<br>"; } //end if upload is hit Okay, now here is the code trying to display the image: <? $featuredquery="SELECT * FROM featured_prods"; $featuredresult=mysql_query($featuredquery, $dbh); while($featuredrow=mysql_fetch_array($featuredresult)){ $id=$featuredrow['id']; $prodquery="SELECT * FROM product WHERE id='$id'"; $prodresult=mysql_query($prodquery, $dbh); $prodrow=mysql_fetch_array($prodresult); echo $prodrow['title']; echo "<br>"; echo $featuredrow['message']; echo "<br>"; ?> <img src="getimage.php?id=<?echo $id;?>" alt="cover" /> <? } And here is the code for getimage.php: <? $id=$_GET["id"]; $result = mysql_query("select image from featured_prods where id='$id'"); $row = mysql_fetch_row($result); $data = base64_decode($row[0]); $im = imagecreatefromstring($row[0]); imagejpeg($im); header('Content-type: ' . image/jpeg); // 'image/jpeg' for JPEG images echo $data; ?> Again, any help would be appreciated. I'm a real rookie here, and I appreciate everyone's time and effort to assist me very much. it should display the image but nothing appears. what have i done wrong? Code: [Select] echo "<img src=\"photo.php?id=$studentid\" alt='photo' />"; photo.php Code: [Select] <?php $id= $_GET['id']; $getphoto= mysql_query("SELECT photo FROM Student WHERE SID=$id LIMIT 1"); $row5 = mysql_fetch_assoc($getphoto); $photogot = $row5['PHOTO']; header("Content-type: image/gif"); print $photogot; ?> Code: [Select] $thephoto = $row['thePHOTO']; in mysql thephoto is BLOB file type and stores an image i want to display the image as an avatar, in a while loop. Code: [Select] while($row = mysql_fetch_assoc($query4)) { $id = $row['ID']; $thename = $row['theNAME']; $thephoto = $row['thePHOTO']; } how do i make blob readable and then display as an image? I have my database set to insert the current time stamp when an entry is made into the table, I am then trying to retrieve via the following code: $select_view_idea="SELECT * FROM $tbl_name5 WHERE message_number='$message_number'"; $result_view_idea=mysql_query($select_view_idea); while($row_view_idea=mysql_fetch_assoc($result_view_idea)){ extract($row_view_idea); } date_default_timezone_set('US/Eastern'); $date=date('l, F jS Y h:i:s A T', $date); echo $date; The above is outputting: Wednesday, December 31st 1969 07:33:31 PM EST the database contains: 2011-11-18 00:47:56 Hey all, I've written a php search feature for a mysql database. The search returns a file name like sample.gif, how would I go about displaying the actual image instead? Since the images all have the same location could I have the search return the string into a variable then make the whole thing a link? Thanks. I using.. I have a base64 encoded image -> http://pastebin.com/698ES5t0
Im trying to export this as a png file.
$picture = {data on paste bin}; $picture = base64_decode($picture); file_put_contents('/home/picture.png', $Picture);Now this creates a file picture.png and i can open it in Preview/Gimp etc. However, if i upload the file to my website, the image does not render in chrome/firefox, however it does render in Safarai. It seems that there is no height/width/depth data. Hi guys, I have a problem. I need to create a page that has a web form to upload an image to a mySQL database and place it in a blob field. Then I need to be able to query the database later to display the image on the site. I've looked around but I just haven't found any examples that can help me. Does anyone know of any good example or can anyone please give me an example? I have nothing so far, just the html web form and database. Hi All, This is my first post on here. I'd really appreciate any help with this, it's driving me mad. I'm trying to create a form to save a image into a mySQL database. I have a website hosted by UK2.net which has a mysql db with a table called gallery(name (varchar 30), size (int), type varchar(30), thePic(mediumBlob)). I have a form with this: Code: [Select] <td><p><label>Pic: </label></td><td><input name="userfile" type="file" id="userfile" /></td></p> Which when submitted actions addImage.php. The code for this looks like this: Code: [Select] <?php $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $con = mysql_connect("localhost", "userName", "password") or die(mysql_error()); mysql_select_db("dbName", $con) or die(mysql_error()); $query = "INSERT INTO gallery (name, size, type, thePic) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; if (!mysql_query($query,$con)) { die('Error: ' . mysql_error()); } echo "<br>File $fileName uploaded<br>"; ?> I get the following error message: Quote Warning: fopen() [function.fopen]: Filename cannot be empty in /home/hiddenje/public_html/addImage.php on line 13 Does anyone know what this is about? I've been on out friend google and a lot of people seem to be pointing to permissions but I can't seem to apply it to my scenario and just can't get it to work. Im a developer by trade, but this is my first step into the...interesting world of PHP and mySQL. Again, I'd appreciate any help with this. I'd love someone to talk me through exactly what I'm missing or doing wrong. Thanks in advance. Code: [Select] echo '<img src="data:image/jpg/png/jpeg;base64,' . base64_encode( $row['image'] ) . '" height="150" />'; This is showing up images great in firefox, safari and chrome, but in internet explorer it shows a nice red cross, and I assume it is because of the encoding? Does anyone know how to get working in internet explorer as well? Pretty urgent job! Much appreciated for your help! Hello people, currently i ran into some problem. Currently, i have a database called responses which have the fields of ID, Student_id, question_id, Answer. I had stored my results into the database which appeared to be 1 1 1 Agree 2 1 2 Disagree 3 3 4 Unsatisfied. So any recommendation on how should i do about in generating the result into a graph whereby the graph will show how many students choose "AGree" on that particular question THANKS I have two tables. Table Name:Users Fields: User_name user_email user_level pwd 2.Reference Fields: refid username origin destination user_name in the users table and the username field in reference fields are common fields. There is user order form.whenever an user places an order, refid field in reference table will be updated.So the user will be provided with an refid Steps: 1.User needs to log in with a valid user id and pwd 2.Once logged in, there will be search, where the user will input the refid which has been provided to him during the time of order placement. 3.Now User is able to view all the details for any refid 3.Up to this we have completed. Query: Now we need to retrieve the details based on the user logged in. For eg: user 'USER A' has been provided with the referenceid '1234' during the time of order placement user 'USER B' has been provided with the referenceid '2468' during the time of order placement When the userA login and enter the refid as '2468' he should not get any details.He should get details only for the reference ids which is assigned to him. I am working on a kind of CMS for my own website which no one else will be using but me as a way of improving my php skills, and am having problems with retrieving data from the database that holds both text and php code. I have searched the web and found that i should be using eval() for the code to be executed before it is send to the browser but cannot get it to work and can't find my mistake(s). the php code will always be the same, and is supposed to retrieve the id number of a page to use in a link (and works fine when tested by loading the code directly without retrieving it from the database) this is an example of data stored in the database Code: [Select] The <a href="page_builder.php?id=<?php echo $page->id('mines_DwarvenMines') ?>">Dwarven Mines</a> have a great selection of Ores,... Of course when I leave it like this, hovering over the link in my page will show exactly that and lead to nowhere Code: [Select] localhost/page_builder.php?id=<?php echo $page->id('mines_DwarvenMines') ?> Most of these links appear in tips given at the end of the page and are processed as followed Code: [Select] $questtips = $quest->getQuestTips(); $tips = ""; if ($questtips == "none") { $tips = "/"; } else { foreach($questtips as $tip) { $tips .= "<li>"; $tips .= $tip->getTip(); $tips .= "</li>"; } } and finally put on screen by the presentation layer as followed Code: [Select] <h2>Tips & Extra Info</h2> <div class="tipsList"> <ul> <?php echo $tips ?> </ul> </div> I have tried all sorts to get the code to be executed when retrieved from the database before being send to the browser so that this particular link would say "localhost/page_builder.php?id=57" but I cannot get it to work, though I suspect it is fairly easy. I suspect I would have to store the data in a different format in the database? And how exactly do I use the eval() function in my case? Could someone please adjust my code so that it does work? Thanks this is my 3 files which is i am using to show data of customer but i am not able to see them their logo image please see them & help me guys i dont know whats wrong in this. these files i uploaded please find attachment to see them. thnx in advanced [attachment deleted by admin] Okay, so here is the deal. Have a table which stores image as blob files. Now i want to read the image width and height directly from the blob field. Is this possible and if yes, how? Things i tried so far; list($size[0],$size[1],$type, $attr) = getimagesize('image.php?i=26ddd45b02859e836d13d4b9fde34281'); print_r($size); $img = 'image.php?i=26ddd45b02859e836d13d4b9fde34281'; echo imagesy($img); image.php grabs the image from DB and show's it with header("Content-type: image/jpg"); It works for just showing the images with the <img> tag. Any ideas of help would be great! |