i am trying to store image in daabse.n display on web page. i have abled to store it in db.but couldnt able to display. So plz help me. the code is this <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head><title>File Upload To Database</title></head> <body> <h2>Please Choose a File and click Submit</h2> <form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post"> <input type="hidden" name="MAX_FILE_SIZE" value="99999999" /> <div><input name="userfile" type="file" /></div> <div><input type="submit" value="Submit" /></div> </form> <?php /*** check if a file was submitted ***/ if(!isset($_FILES['userfile'])) { echo '<p>Please select a file</p>'; } else { try { upload(); /*** give praise and thanks to the php gods ***/ echo '<p>Thank you for submitting</p>'; } catch(Exception $e) { echo '<h4>'.$e->getMessage().'</h4>'; } } ?> <?php /** * * the upload function * * @access public * * @return void * */ function upload(){ /*** check if a file was uploaded ***/ if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false) { /*** get the image info. ***/ $size = getimagesize($_FILES['userfile']['tmp_name']); /*** assign our variables ***/ $type = $size['mime']; $imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb'); $size = $size[3]; $name = $_FILES['userfile']['name']; $maxsize = 99999999; /*** check the file is less than the maximum file size ***/ if($_FILES['userfile']['size'] < $maxsize ) { /*** connect to db ***/ $dbh = new PDO("mysql:host=localhost;dbname=test", 'root', ''); /*** set the error mode ***/ $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /*** our sql query ***/ $stmt = $dbh->prepare("INSERT INTO testblob (image_type ,image, image_size, image_name) VALUES (? ,?, ?, ?)"); /*** bind the params ***/ $stmt->bindParam(1, $type); $stmt->bindParam(2, $imgfp, PDO:ARAM_LOB); $stmt->bindParam(3, $size); $stmt->bindParam(4, $name); /*** execute the query ***/ $stmt->execute(); } else { /*** throw an exception is image is not of type ***/ throw new Exception("File Size Error"); } } else { // if the file is not less than the maximum allowed, print an error throw new Exception("Unsupported Image Format!"); } } ?> <?php // $sql = "SELECT max(image_id)FROM testblob; //$stmt = $dbh->prepare($sql); //$stmt->execute(); //$row = mysql_fetch_array($stmt); // &imageId = $row[0]; //echo &imageId; // $sq2= "SELECT *FROM testblob where image_id=&imageId"; //$stmt = $dbh->prepare($sq2); //$stmt->execute(); /*** some basic sanity checks ***/ // 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", "", "") 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=5"; // 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); // close the db link mysql_close($link); } else { echo 'Please use a real id number'; } ?> </body></html> output: Please Choose a File and click Submit Please select a file Please use a real id number
Display Your Images With Given Code... Please customize it before use.. <?php // ............ Database connection variables will be here STARTs................// $hostname = ""; // Put user server name here $username = ""; // Put user name here $password = ""; // Put password name here $database = ""; // Put database name here // ............ Database connection variables will be here ENDS................// $Conn_db = mysql_connect($hostname, $username, $password) or die(mysql_error()); // connecting to database mysql_select_db($database, $Conn_db); // Selecting Database $sql = "SELECT * FROM IMAGETABLE"; // MySQL commant to select records from IMAGE TABLE $res = mysql_query($sql); // Executing query // WHile loop to bring all specified results... while($rows = mysql_fetch_assoc($res) ){ // Fetching records $FilePATH = ABSOLUTE_PATH.$rows['IMAGENAME']; // make path where you stored images echo '<img src="'.$FilePATH.'"> <br>'; // Showing images .. <br> for new line } ?> Code (markup):
Sir first of all most thanks to you for replying.And helping me. but i m having these errors by ur code. Notice: Use of undefined constant ABSOLUTE_PATH - assumed 'ABSOLUTE_PATH' in C:\wamp\www\work\newsqlpic.php on line 167 Notice: Undefined index: IMAGENAME in C:\wamp\www\work\newsqlpic.php on line 167
Now Error is like this... 1.Notice: Use of undefined constant ABSOLUTE_PATH - assumed 'ABSOLUTE_PATH' in C:\wamp\www\work\newsqlpic.php on line 167 <µÌÂFjÂ,q[<“à 0?($b³$º6ï1E˱ªŸÚW;–Âʤ`€~éë™SoSndž¥ûw7r<‰Ê•*yéYïlð¡â€Â«dþTÃ>éÞ‡;01½¹Îp>ŸÂ\†SsïN÷ª³‹fE}kt/"y¿7 §Mb"Œ‡ÂÈVB ÿ�õª}[UŽX »uÃ#iÛõÂJÃKÕ‰ÃVxÃû' Ö‘nÄI+–EÇÙ.Ã’`wªýáŒqßô&¯™ÂO Ίg‚;ŽÆ³%ºµ¸å¡u'øÂÂúT1ÉQ)rø9Æ)+Ž.ÚõN4ô¦{ÖûŠ*Ñ™'cW.dv³¶‰h¢“)ÿÙ"> 2.Notice: Use of undefined constant ABSOLUTE_PATH - assumed 'ABSOLUTE_PATH' in C:\wamp\www\work\newsqlpic.php on line 167 <µÌÂFjÂ,q[<“à 0?($b³$º6ï1E˱ªŸÚW;–Âʤ`€~éë™SoSndž¥ûw7r<‰Ê•*yéYïlð¡â€Â«dþTÃ>éÞ‡;01½¹Îp>ŸÂ\†SsïN÷ª³‹fE}kt/"y¿7 §Mb"Œ‡ÂÈVB ÿ�õª}[UŽX »uÃ#iÛõÂJÃKÕ‰ÃVxÃû' Ö‘nÄI+–EÇÙ.Ã’`wªýáŒqßô&¯™ÂO Ίg‚;ŽÆ³%ºµ¸å¡u'øÂÂúT1ÉQ)rø9Æ)+Ž.ÚõN4ô¦{ÖûŠ*Ñ™'cW.dv³¶‰h¢“)ÿÙ">
Sorry for inconvenience. YOu can define.. <?php define("ABSOLUTE_PATH","http://www.yoursitename.com"); ?> Notice: Undefined index: IMAGENAME in C:\wamp\www\work\newsqlpic.php on line 167 *************************************************************** IMAGENAME is column name of your table where you r storing image name. Replace IMAGENAME with column name. This was general code.. so change according to your server settings. Thanks and Best Wishes