Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- database info:
- -------------
- -------------
- host : localhost
- user : root
- pass :''
- database: picture
- table name: getimage
- coumn no: 2
- id : int auto increment
- image: longblob
- -----------------
- ----------------
- db.php:
- -------
- -------
- <?php
- define( 'HOST', 'localhost' );
- define( 'USER', 'root' );
- define( 'PASSWORD', '' );
- define( 'DB', 'picture' );
- $connect = mysqli_connect( HOST, USER, PASSWORD, DB );
- if ( !$connect ) {
- echo mysqli_connect_error($connect);
- }
- ?>
- -----
- -----
- main.php:
- ---------
- ---------
- <?php
- include 'db.php';
- ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>getting image</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
- <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>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
- <style media="screen">
- #image{
- }
- #image img{
- border: 1px solid none;
- border-radius: 30%;
- margin: 5px;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="row">
- <div class="col-md-12" style="margin-top:20px;">
- <div id="image">
- <!-- get all image from database -->
- <?php
- $select = "select image from getimage";
- $result = mysqli_query( $connect, $select );
- if ( $result) {
- if ( mysqli_num_rows( $result ) > 0 ) {
- while ( $row = mysqli_fetch_array($result) ) {
- echo '<img alt="" width="200" height="200" src="data:image;base64,'.$row['image'].'">';
- }
- }
- }
- ?>
- </div>
- </div>
- <div class="col-md-6" style="margin-top:20px;">
- <form action="" method="post" enctype="multipart/form-data">
- <div class="form-group">
- <label for="myImg">upload image</label>
- <input type="file" class="form-control-file" name="myImg" id="myImg" placeholder="" aria-describedby="fileHelpId">
- </div>
- <button type="submit" class="btn btn-primary btn-sm" name="sub" id="sub">Submit</button>
- <button type="reset" class="btn btn-dark btn-sm">clear</button>
- </form>
- </div>
- <?php
- if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
- if ( isset( $_POST['sub'] ) ) {
- $uploadedImageName = addslashes( $_FILES["myImg"]["name"] );
- $tempFile = addslashes( $_FILES['myImg']['tmp_name'] ) ;
- $file = file_get_contents( $tempFile ) ;
- $file = base64_encode( $file );
- $folder = 'images/'.$tempFile;
- move_uploaded_file( $tempFile, $folder );
- $insert = "insert into getimage(image) values('$file')";
- if ( mysqli_query( $connect, $insert ) ) {
- echo '<div class="col-md-6" style="margin-top:20px;">
- <div class="alert alert-success" role="alert">
- <a href="" class="close" data-dismiss="alert">×</a>
- <p class="alert-heading">file uploaded</p>
- </div>
- </div>';
- }
- }
- }
- ?>
- </div>
- </div>
- <script>
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement