Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.57 KB | None | 0 0
  1. <?php
  2. $user='<user>';
  3. $password='<pass>';
  4. $database='<db>';
  5. $host='localhost';
  6.  
  7. $conn=mysql_connect($host,$user,$password) or die(mysql_error());
  8. mysql_select_db($database) or die(mysql_error());
  9. ?>
  10. <html>
  11. <head><title>Store binary data into SQL Database</title></head>
  12. <body>
  13.  
  14. <?php
  15. // code that will be executed if the form has been submitted:
  16.  
  17. if (isset($_POST['submit'])) {
  18.  
  19.     // connect to the database
  20.     $tmp = $_FILES['form_data']['tmp_name'];
  21.     $imgFormat = $_FILES['form_data']['type'];
  22.    
  23.     $fi = fopen($tmp,'r');
  24.     $img = fread($fi,filesize($tmp));
  25.     $img = addslashes($img);
  26.     fclose($fi);
  27.    
  28.     $query = "INSERT INTO masti (caption, IMG) VALUES ('$imgFormat', '$img')";
  29.     mysql_query($query) or die();
  30.    
  31.     $query = "SELECT img FROM masti WHERE id=1";
  32.     $res = mysql_query($query) or die();
  33.     $val = mysql_fetch_assoc($res);
  34.    
  35.     $frm = "Content-type: ".$imgFormat."";
  36.     $Len = "Content-length: ".strlen($val['img'])."";
  37.     header($Len);
  38.     header($frm);
  39.         echo $val['img'];
  40.         exit();
  41.     //echo "<img src='".$val['img']."' />";
  42.    
  43.    
  44. } else {
  45.  
  46.     // else show the form to submit new data:
  47. ?>
  48.  
  49.     <form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data">
  50.     File Description:<br>
  51.     <input type="text" name="form_description"  size="40">
  52.     <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  53.     <br>File to upload/store in database:<br>
  54.     <input type="file" name="form_data"  size="40">
  55.     <p><input type="submit" name="submit" value="submit"></p>
  56.     </form>
  57.  
  58. <?php
  59.  
  60. }
  61.  
  62. ?>
  63.  
  64. </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement