contra10 Posted January 4, 2009 Share Posted January 4, 2009 hey i want to read an image file i uploaded into database <?php $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $ii = "{$row['content']} <br>"; echo($ii); } echo($ii); ?> how di i see the image Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 4, 2009 Author Share Posted January 4, 2009 the image file is in the database but it comes out as binary, how do i convert it back? Quote Link to comment Share on other sites More sharing options...
premiso Posted January 4, 2009 Share Posted January 4, 2009 You have to set the page headers. header Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 4, 2009 Author Share Posted January 4, 2009 am i srtof on the right track <?php if(is_numeric($_GET['id'])){ $id = $_GET['id']; $insert1= "SELECT * FROM users WHERE id = '$id'"; $idnu = mysql_query($insert1) or die(mysql_error()); while ($idn = mysql_fetch_assoc($idnu)) { $idnumb= "{$idn['id']}"; } } header("Location: http://localhost/photoupload/index.php?userphotoid=$idnumb"); $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $ii = "{$row['content']} <br>"; echo($ii); } echo($ii); ?> Quote Link to comment Share on other sites More sharing options...
premiso Posted January 4, 2009 Share Posted January 4, 2009 No, you do not want to the location. The header tag has many more uses. Look at the header site for other examples of "Content-type:" <?php if(is_numeric($_GET['id'])){ $id = $_GET['id']; $insert1= "SELECT * FROM users WHERE id = '$id'"; $idnu = mysql_query($insert1) or die(mysql_error()); while ($idn = mysql_fetch_assoc($idnu)) { $idnumb= "{$idn['id']}"; } } $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); // We'll be outputting a jpeg header('Content-type: application/jpeg'); // this needs to be changed to the actual type of the image, png, gif, jpeg etc echo $row['content']; ?> I do not know if that will work but that is the basic idea. Read through the WHOLE page of header and you will see plenty of examples, especially in the user remarks. Remember the first example on PHP.NET is not always the right one or the example you need, so read through the WHOLE page. Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 4, 2009 Author Share Posted January 4, 2009 oh ok, i did that and i changed it around and when i click on the link to the page a get a window that asks me to save the file index.php i guess its because the image did upload to the page but i would have to download it? Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 <?php $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); // We'll be outputting a jpeg header("Content-type: img/pjpeg"); // this needs to be changed to the actual type of the image, png, gif, jpeg etc echo $row['content']; ?> Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 edit: removed. doing more research... http://www.google.com/search?hl=en&q=php+display+image+mysql&btnG=Google+Search&aq=f&oq=php+display+image+mysq http://www.phpriot.com/articles/images-in-mysql Will help you out. Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 thank u it looks pretty useful! Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 To fix the error in the code I posted: // We'll be outputting a jpeg header('Content-type: image/jpeg'); // this needs to be changed to the actual type of the image, png, gif, jpeg etc echo $row['content']; I just had the content-type wrong, should be image/jpeg. Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 just to note i did that prviously, didn't work...but im using the resource website that you found and on page 7 it basically says this <?php // the upload function function upload(){ if(is_uploaded_file($_FILES['userfile']['tmp_name'])) { // check the file is less than the maximum file size if($_FILES['userfile']['size'] < $maxsize) { // prepare the image for insertion $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name'])); // $imgData = addslashes($_FILES['userfile']); // get the image info.. $size = getimagesize($_FILES['userfile']['tmp_name']); // put the image in the db... mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); // our sql query $sql = "INSERT INTO testblob ( image_id , image_type ,image, image_size, image_name) VALUES ('', '{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name']}')"; // insert the image if(!mysql_query($sql)) { echo 'Unable to upload file'; } } } else { // if the file is not less than the maximum allowed, print an error echo '<div>File exceeds the Maximum File limit</div> <div>Maximum File limit is '.$maxsize.'</div> <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].' bytes</div> <hr />'; } } ?> with the exception that i put my databse connection in there... when i submit the data does not enter the table and i get this mesage File exceeds the Maximum File limit Maximum File limit is File Array is Array bytes -------------------------------------------------------------------------------- Thank you for submitting any ideas? Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 the code before the switch to this one was workin well but i could not view the files, a pop-up came on my screen saying that i had to downloaod file Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 I did not really mean for you to actually implement the whole script. More of use it as a guideline, particulary the display part. <?php // just so we know it is broken error_reporting(E_ALL); // some basic sanity checks if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) { //connect to the db $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error()); // select our database mysql_select_db("testblob") or die(mysql_error()); // get the image from the db $sql = "SELECT image FROM testblob WHERE image_id=0"; // the result of the query $result = mysql_query("$sql") or die("Invalid query: " . mysql_error()); // set the header for the image header("Content-type: image/jpeg"); echo mysql_result($result, 0); } else { echo 'Please use a real id number'; } ?> As seen there, my implementation of the header content-type was correct. So back to your old code: <?php $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); header("Content-type: image/jpeg"); echo($row['content']); ?> And on a huge SIDE NOTE!!!:::::: The image must be a JPEG type that was inserted into the DB for this to work. If it was png it should be image/png or gif it should be image/gif. If you do not know what type it was I would suggest looking into storing the type when it is uploaded in the DB so you can call the correct content-type when the time is necessary. Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 yes u were definatly right with the content... i have <?php $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); header("Content-type: image/jpeg"); echo($row['content']); ?> i also tried <?php $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); header("Content-type: image/pjpeg"); echo($row['content']); ?> since it was pjpeg in the database, i still get ÿØÿàJFIFÿÛC "" $(4,$&1'-=-157:::#+?D?8C49:7ÿÛC 7%%77777777777777777777777777777777777777777777777777ÿÀ00"ÿÄÿÄ.!1AQq"#B¡±$2b‘ÿÄÿÄ!1QÿÚ ?ÎQ‘/Ú¼‰`(ál‡Gî#oZÓý-%¬KYdU'ÎŽ é ðEj¦W$û“Ú$[#¼‘ÝËwb×üVwdo’fظ0Á4Ÿ Ø¿ÕÅ-âùûzÜsC.âºáí¤µ ø§±ÜФ;SV;Ué]ˆˆæÛPTíEFRlÜwªJJ1m’¾Y¦tÿWá±ø8g›1Ž UBK^±ä7í\‡TæSf™¬’sd…CôžçÔÛíUM6Ål$†‘4U¨¾ö=»ÿ´¸Tðc䑵…ïYê#ÓwdFvØìE‘ÜÐœÔÜB«Æîƒâ[ŸQTÒâ §¾6GPºqõx>Âœ%ÒÊOª?ˆ§Jß oü«*̯ÒiŽÈŸâ€wX½#ÊéñFnFà_jŽ¯hÁ;©µÍ1Ée ƒ¸¥AeŒ¿'b-\ô„«2·*H«¬,ÀéÔqÈÛš£ÅÔÌ/õ·æŠãK5±nÿÙ i really don't know y this can't work for me, everything your doing is right and i thought it was correct here's my whole code at the moment <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); 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']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } $query = "INSERT INTO image (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> </body> </html> <table border="1" cellpadding="10"> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </form> </table> <?php $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); header("Content-type: image/pjpeg"); echo($row['content']); ?> </tr> </form> </table> <body> </html> files can go in but they can't come out.... i don't know if this helps but i uploaded 2 images and already my id count is up to 4 i think each image is taking up 2 id fields Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 NVM id did not take up 2 fields Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 Why are you adding slashes? Try removing the add slashes and sees what happens. If you need to escape it use mysql_real_escape_string over add slashes. Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 i did this <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); 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']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = mysql_real_escape_string($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = mysql_real_escape_string($fileName); } $query = "INSERT INTO image (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> </body> </html> <table border="1" cellpadding="10"> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </form> </table> <?php $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); header("Content-type: image/pjpeg"); echo($row['content']); ?> and got the same thing as before and i also did this <?php mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("registration") or die(mysql_error()); 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']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); fclose($fp); $query = "INSERT INTO image (name, size, type, content ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$content')"; mysql_query($query) or die('Error, query failed'); echo "<br>File $fileName uploaded<br>"; } ?> </body> </html> <table border="1" cellpadding="10"> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"> <table width="350" border="0" cellpadding="1" cellspacing="1" class="box"> <tr> <td width="246"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> </td> <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </form> </table> <?php $query = "SELECT content FROM image WHERE id= '1'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); header("Content-type: image/pjpeg"); echo($row['content']); ?> and got "query error". Quote Link to comment Share on other sites More sharing options...
premiso Posted January 5, 2009 Share Posted January 5, 2009 "an got query error" How does that help? Change this: mysql_query($query) or die('Error, query failed'); to mysql_query($query) or die('Error, query failed!<br />' . mysql_error()); And report back that error. Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 i get Error, query failed! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-=-157:::#+?D?8C49:7ÿÛ' at line 1 sry if im bothering u Quote Link to comment Share on other sites More sharing options...
contra10 Posted January 5, 2009 Author Share Posted January 5, 2009 well thanks for trying, ill jus have to figure out another way 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.