Advertisement
Riju18

no.98_get_image_from_database_base64_encoding

Jun 18th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.90 KB | None | 0 0
  1. database info:
  2. -------------
  3. -------------
  4. host : localhost
  5. user : root
  6. pass :''
  7. database: picture
  8. table name: getimage
  9. coumn no: 2
  10.       id : int auto increment
  11.       image: longblob
  12. -----------------
  13. ----------------
  14. db.php:
  15. -------
  16. -------
  17. <?php
  18.   define( 'HOST', 'localhost' );
  19.   define( 'USER', 'root' );
  20.   define( 'PASSWORD', '' );
  21.   define( 'DB', 'picture' );
  22.  
  23.   $connect = mysqli_connect( HOST, USER, PASSWORD, DB );
  24.   if ( !$connect ) {
  25.     echo mysqli_connect_error($connect);
  26.   }
  27.  ?>
  28. -----
  29. -----
  30. main.php:
  31. ---------
  32. ---------
  33. <?php
  34.   include 'db.php';
  35.  ?>
  36. <!DOCTYPE html>
  37. <html lang="en">
  38. <head>
  39.   <meta charset="UTF-8">
  40.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  41.   <meta http-equiv="X-UA-Compatible" content="ie=edge">
  42.   <title>getting image</title>
  43.   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  44.   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
  45.   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  46.   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  47.   <style media="screen">
  48.     #image{
  49.  
  50.     }
  51.     #image img{
  52.       border: 1px solid none;
  53.       border-radius: 30%;
  54.       margin: 5px;
  55.     }
  56.   </style>
  57. </head>
  58. <body>
  59.  
  60.   <div class="container">
  61.     <div class="row">
  62.       <div class="col-md-12" style="margin-top:20px;">
  63.       <div id="image">
  64.         <!-- get all image from database -->
  65.         <?php
  66.         $select = "select image from getimage";
  67.         $result = mysqli_query( $connect, $select );
  68.         if ( $result) {
  69.           if ( mysqli_num_rows( $result ) > 0 ) {
  70.             while ( $row = mysqli_fetch_array($result) ) {
  71.               echo '<img alt="" width="200" height="200" src="data:image;base64,'.$row['image'].'">';
  72.             }
  73.           }
  74.          
  75.         }
  76.          ?>
  77.       </div>
  78.       </div>
  79.       <div class="col-md-6" style="margin-top:20px;">
  80.  
  81.         <form action="" method="post" enctype="multipart/form-data">
  82.           <div class="form-group">
  83.             <label for="myImg">upload image</label>
  84.             <input type="file" class="form-control-file" name="myImg" id="myImg" placeholder="" aria-describedby="fileHelpId">
  85.           </div>
  86.           <button type="submit" class="btn btn-primary btn-sm" name="sub" id="sub">Submit</button>
  87.           <button type="reset" class="btn btn-dark btn-sm">clear</button>
  88.         </form>
  89.  
  90.       </div>
  91.  
  92.       <?php
  93.         if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
  94.           if ( isset( $_POST['sub'] ) ) {
  95.  
  96.             $uploadedImageName = addslashes( $_FILES["myImg"]["name"] );
  97.             $tempFile          = addslashes( $_FILES['myImg']['tmp_name'] ) ;
  98.             $file              = file_get_contents( $tempFile ) ;
  99.             $file              = base64_encode( $file );
  100.             $folder            = 'images/'.$tempFile;
  101.  
  102.             move_uploaded_file( $tempFile, $folder );
  103.  
  104.             $insert = "insert into getimage(image) values('$file')";
  105.             if ( mysqli_query( $connect, $insert ) ) {
  106.               echo '<div class="col-md-6" style="margin-top:20px;">
  107.                <div class="alert alert-success" role="alert">
  108.                  <a href="" class="close" data-dismiss="alert">&times;</a>
  109.                  <p class="alert-heading">file uploaded</p>
  110.                </div>
  111.              </div>';
  112.             }
  113.           }
  114.         }
  115.        ?>
  116.  
  117.     </div>
  118.   </div>
  119.   <script>
  120.  
  121.    </script>
  122. </body>
  123. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement