Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Photo Upload and Retrive with MySql using BLOB in PHP

Creating a table to store the image in terms of bytes.
Create Database with specific table as
>>create database image;
>>create table storeimages(id int not null auto increment,primary key(id),name varchar(50) , image longblob , size int );

Output
--------
>>Table Created Successfully



HTML and CSS is added with PHP
Store File in a name : UploadPhoto.php
  1.  
  2. <html>
  3. <head><title>Upload - photo</title>
  4. <style>
  5. .up_pho
  6. {
  7. border:1px solid #BEBEBE;
  8. margin:50px 400px 40px 40px;
  9. padding:10px 10px 10px 10px;
  10.  
  11. }
  12.  
  13. </style>
  14. </head>
  15. <body>
  16. <div class=up_pho ><?php
  17. $rollno = rollno;
  18. ?>
  19. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=post enctype="multipart/form-data" >
  20. <input type=hidden name="MAX_FILE_SIZE" value="1000000" />
  21. <input type=submit value=submit />
  22. </form>
  23. <?php
  24.  
  25. // Checking the file was submitted
  26. if(!isset($_FILES['userfile'])) { echo '<p>Please Select a file</p>'; }
  27.  
  28. else
  29. { try {
  30. $msg = upload(); // function calling to upload an image
  31. echo $msg;
  32. }
  33. catch(Exception $e) {
  34. echo $e->getMessage();
  35. echo 'Sorry, Could not upload file';
  36. }
  37. }
  38.  
  39.  
  40. function upload() {
  41. include "database/dbco.php";
  42. $maxsize = 10000000; //set to approx 10 MB
  43.  
  44. //check associated error code
  45. if($_FILES['userfile']['error']==UPLOAD_ERR_OK) {
  46.  
  47. //check whether file is uploaded with HTTP POST
  48. if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {
  49.  
  50. //checks size of uploaded image on server side
  51. if( $_FILES['userfile']['size'] < $maxsize) {
  52.  
  53. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  54.  
  55. //checks whether uploaded file is of image type
  56. if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {
  57.  
  58. // prepare the image for insertion
  59. $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));
  60.  
  61. // put the image in the db...
  62. // database connection
  63. $host = '127.0.0.1';
  64. $user = 'root';
  65. $pass = 'your_password';
  66. $db = 'database_name';
  67.  
  68. mysql_connect($host, $user, $pass) OR DIE (mysql_error());
  69.  
  70. // select the db
  71. mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error());
  72.  
  73. // our sql query
  74.  
  75. $sql = "INSERT INTO storeimages
  76. (id,image, name,size)
  77. VALUES
  78. ('$_POST[id]','{$imgData}', '{$_FILES['userfile']['name']}','{$_FILES['userfile']['size']}');";
  79. mysql_query($sql) or die("Error in Query insert: " . mysql_error());
  80. // insert the image
  81.  
  82. $msg='<p>Image successfully saved in database . </p>';
  83. }
  84. else
  85. $msg="<p>Uploaded file is not an image.</p>";
  86. }
  87. else {
  88. // if the file is not less than the maximum allowed, print an error
  89. $msg='<div>File exceeds the Maximum File limit</div>
  90. <div>Maximum File limit is '.$maxsize.' bytes</div>
  91. <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].
  92. ' bytes</div><hr />';
  93. }
  94. }
  95. else
  96. $msg="File not uploaded successfully.";
  97.  
  98. }
  99. else {
  100. $msg= file_upload_error_message($_FILES['userfile']['error']);
  101. }
  102. return $msg;
  103. }
  104.  
  105. // Function to return error message based on error code
  106.  
  107. function file_upload_error_message($error_code) {
  108. switch ($error_code) {
  109. case UPLOAD_ERR_INI_SIZE:
  110. return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
  111. case UPLOAD_ERR_FORM_SIZE:
  112. return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
  113. case UPLOAD_ERR_PARTIAL:
  114. return 'The uploaded file was only partially uploaded';
  115. case UPLOAD_ERR_NO_FILE:
  116. return 'No file was uploaded';
  117. case UPLOAD_ERR_NO_TMP_DIR:
  118. return 'Missing a temporary folder';
  119. case UPLOAD_ERR_CANT_WRITE:
  120. return 'Failed to write file to disk';
  121. case UPLOAD_ERR_EXTENSION:
  122. return 'File upload stopped by extension';
  123. default:
  124. return 'Unknown upload error';
  125. }
  126. }
  127. ?>
  128. </div>
  129.  
  130. </body>
  131. </html>
  132.  




  1. <?php
  2.  
  3. // Store File in a name : DisplayPhoto.php
  4.  
  5. $host = '127.0.0.1';
  6. $user = 'root';
  7. $pass = 'your_password';
  8. $db = 'database_name';
  9.  
  10. // some basic sanity checks
  11. if(isset($_GET['id']) && is_numeric($_GET['id'])) {
  12. //connect to the db
  13. $link = mysql_connect("$host", "$user", "$pass")
  14. or die("Could not connect: " . mysql_error());
  15.  
  16. // select our database
  17. mysql_select_db("$db") or die(mysql_error());
  18.  
  19. // get the image from the db
  20. $sql = "SELECT image FROM storeimages WHERE id=" .$_GET['id'] . ";";
  21.  
  22. // the result of the query
  23. $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
  24.  
  25. // set the header for the image
  26. header("Content-type: image/jpeg");
  27. echo mysql_result($result, 0);
  28.  
  29. // close the db link
  30. mysql_close($link);
  31. }
  32. else {
  33. echo 'Please use a real id number';
  34. }
  35. ?>
  36.