Monday, 15 March 2010

php - I ensure database that has image and the database connect is correct ,but I cannot display the image -



php - I ensure database that has image and the database connect is correct ,but I cannot display the image -

my display code :

<?php include "file_constants.php"; // know broken error_reporting(e_all); // basic sanity checks if(isset($_get['id']) && is_numeric($_get['id'])) { //connect db $link = mysql_connect("$host", "$user", "$pass") or die("could not connect: " . mysql_error()); // select our database mysql_select_db("$db") or die(mysql_error()); // image db $sql = "select image test_image id=" .$_get['id'] . ";"; // result of query $result = mysql_query("$sql") or die("invalid query: " . mysql_error()); // set header image header("content-type: image/jpeg"); echo mysql_result($result, 0); // close db link mysql_close($link); } else { echo 'please utilize real id number'; } ?>

i ensure database has image , database connect correct. can upload image php phpmyadmin (mysql). however, cannot display image.(http:// /file_display.php?id=1) can help me display image in php?? give thanks much!

the file-insert code:

<html> <head><title>file insert</title></head> <body> <h3>please take file , click submit</h3> <form enctype="multipart/form-data" action= "<?php echo $_server['php_self']; ?>" method="post"> <input type="hidden" name="max_file_size" value="10000000" /> <input name="userfile" type="file" /> <input type="submit" value="submit" /> </form> <?php // check if file submitted if(!isset($_files['userfile'])) { echo '<p>please select file</p>'; } else { seek { $msg= upload(); //this upload image echo $msg; //message showing success or failure. } catch(exception $e) { echo $e->getmessage(); echo 'sorry, not upload file'; } } // upload function function upload() { include "file_constants.php"; $maxsize = 10000000; //set approx 10 mb //check associated error code if($_files['userfile']['error']==upload_err_ok) { //check whether file uploaded http post if(is_uploaded_file($_files['userfile']['tmp_name'])) { //checks size of uploaded image on server side if( $_files['userfile']['size'] < $maxsize) { //checks whether uploaded file of image type //if(strpos(mime_content_type($_files['userfile']['tmp_name']),"image")===0) { $finfo = finfo_open(fileinfo_mime_type); if(strpos(finfo_file($finfo, $_files['userfile']['tmp_name']),"image")===0) { // prepare image insertion $imgdata =addslashes (file_get_contents($_files['userfile']['tmp_name'])); // set image in db... // database connection mysql_connect($host, $user, $pass) or die (mysql_error()); // select db mysql_select_db ($db) or die ("unable select db".mysql_error()); // our sql query $sql = "insert test_image (image, name) values ('{$imgdata}', '{$_files['userfile']['name']}');"; // insert image mysql_query($sql) or die("error in query: " . mysql_error()); $msg='<p>image saved in database id ='. mysql_insert_id().' </p>'; } else $msg="<p>uploaded file not image.</p>"; } else { // if file not less maximum allowed, print error $msg='<div>file exceeds maximum file limit</div> <div>maximum file limit '.$maxsize.' bytes</div> <div>file '.$_files['userfile']['name'].' '.$_files['userfile']['size']. ' bytes</div><hr />'; } } else $msg="file not uploaded successfully."; } else { $msg= file_upload_error_message($_files['userfile']['error']); } homecoming $msg; } // function homecoming error message based on error code function file_upload_error_message($error_code) { switch ($error_code) { case upload_err_ini_size: homecoming 'the uploaded file exceeds upload_max_filesize directive in php.ini'; case upload_err_form_size: homecoming 'the uploaded file exceeds max_file_size directive specified in html form'; case upload_err_partial: homecoming 'the uploaded file partially uploaded'; case upload_err_no_file: homecoming 'no file uploaded'; case upload_err_no_tmp_dir: homecoming 'missing temporary folder'; case upload_err_cant_write: homecoming 'failed write file disk'; case upload_err_extension: homecoming 'file upload stopped extension'; default: homecoming 'unknown upload error'; } } ?> </body> </html>

the sql is:

create table test_image ( id int(10) not null auto_increment primary key, name varchar(25) not null default '', image blob not null );

the tutorial http://vikasmahajan.wordpress.com/2010/07/07/inserting-and-displaying-images-in-mysql-using-php/

if saved image name in database , display image in html

<?php $result = mysql_query("$sql") or die("invalid query: " . mysql_error()); while($row = mysqli_fetch_array($result)) { echo '<img src="www.yourdomain.com/your/directory/"'. $row["image "].'/>'; } ?>

note : dont need utilize while loop if having 1 row

php mysql image

No comments:

Post a Comment