Monday, March 27, 2017

preview before uploading images using bootstrap imageupload form in jquery and php pdo.

How to preview before uploading  images using bootstrap imageupload form in jquery and php pdo.


I have already discuss how to upload images in our previous tutorial.
Now in this tutorial, i am just going to show you how we can preview before uploading images with using bootstrap plugin for uploading images.

You can download it freely here from ;github for this plugin.
Now Lets Start,First create database first "upload"


 Table structure for table `images`
 

CREATE TABLE IF NOT EXISTS `images` (
  `id` int(14) NOT NULL,
  `image` varchar(123) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

File path should be like this

  • index.php file
  • db.php file
  • create.php file
  • uploads folder

db.php file
  1. <?php
  2. date_default_timezone_set("Asia/Kathmandu");
  3.     $db_host = "localhost";
  4.     $db_name = "upload";
  5.     $db_user = "root";
  6.     $db_pass = "";
  7.     
  8.     try{
  9.         
  10.         $db_con = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
  11.         $db_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.     }
  13.     catch(PDOException $e){
  14.         echo $e->getMessage();
  15.     }
  16. ?>

index.php file

Here we create form and  used this plugin as like this

  1.  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>onlinestudy.bramento.com></title>
  7. <link href="../bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
  8. <link rel="stylesheet" href="../css/style.css" type="text/css" />
  9. <script type="text/javascript" src="../assets/jquery-1.11.3-jquery.min.js"></script>
  10. <script src="../bootstrap/js/bootstrap.min.js"></script>
  11. <!--this is a plugin i have used to display images before uploading with pdo --> 
  12. <link href="../assets/dist/css/bootstrap-imageupload.css" rel="stylesheet">
  13.         <style>
  14.            
  15.             .imageupload {
  16.                 margin: 20px 0;
  17.             }
  18.         </style>
  19. </head>
  20. <body>
  21.  <form method="post" action="create.php" enctype="multipart/form-data">
  22.     <!-- bootstrap-imageupload. -->
  23.             <div class="imageupload panel panel-default">
  24.                 <div class="panel-heading clearfix">
  25.                     <h3 class="panel-title pull-left">Upload Image</h3>
  26.                     <div class="btn-group pull-right">
  27.                         <button type="button" class="btn btn-default active">File</button>
  28.                         <button type="button" class="btn btn-default">URL</button>
  29.                     </div>
  30.                 </div>
  31.                 <div class="file-tab panel-body">
  32.                     <label class="btn btn-default btn-file">
  33.                         <span>Browse</span>
  34.                         <!-- The file is stored here. -->
  35.                         <input type="file" name="image">
  36.                     </label>
  37.                     <button type="button" class="btn btn-default">Remove</button>
  38.                 </div>
  39.                 <div class="url-tab panel-body">
  40.                     <div class="input-group">
  41.                         <input type="text" class="form-control hasclear" placeholder="Image URL">
  42.                         <div class="input-group-btn">
  43.                             <button type="button" class="btn btn-default">Submit</button>
  44.                         </div>
  45.                     </div>
  46.                     <button type="button" class="btn btn-default">Remove</button>
  47.                     <!-- The URL is stored here. -->
  48.                     <input type="hidden" name="image">
  49.                 </div>
  50.             </div>
  51.             <!-- bootstrap-imageupload method buttons. -->
  52.             <button type="button" id="imageupload-disable" class="btn btn-danger">Disable</button>
  53.             <button type="button" id="imageupload-enable" class="btn btn-success">Enable</button>
  54.             <button type="button" id="imageupload-reset" class="btn btn-primary">Reset</button>
  55.         </div>
  56.  <button type="submit" class="btn btn-primary" name="Submit" id="btn-save">
  57.             <span class="glyphicon glyphicon-plus"></span> Save this Record
  58.             </button>  
  59. </form>
  60. <!-- include this too, its a js of  bootstrap image upload-->
  61. <script src="../assets/dist/js/bootstrap-imageupload.js"></script>
  62.       <script>
  63.             var $imageupload = $('.imageupload');
  64.             $imageupload.imageupload();
  65.             $('#imageupload-disable').on('click', function() {
  66.                 $imageupload.imageupload('disable');
  67.                 $(this).blur();
  68.             })
  69.             $('#imageupload-enable').on('click', function() {
  70.                 $imageupload.imageupload('enable');
  71.                 $(this).blur();
  72.             })
  73.             $('#imageupload-reset').on('click', function() {
  74.                 $imageupload.imageupload('reset');
  75.                 $(this).blur();
  76.             });
  77.         </script>
  78. </body>
  79. </html>

NOw finally create.php file

This file is for php scripts for image uploading process

  1. <?php
    require_once 'db.php';  
  2.  
  3.  if(isset($_POST['Submit']))
  4.     {
  5.         
  6.         $images=$_FILES["image"]["name"];
  7. $image_tmp = $_FILES['image']['tmp_name'];
  8.     if($images=='')    {
  9.         echo "<script>alert('Please Choose Images')</script>";    
  10.         echo "<script>window.open('index.php','_self')</script>";    
  11.     }
  12. else {    try{
  13.                move_uploaded_file($image_tmp,"uploads/$images");
  14.             $stmt = $db_con->prepare("INSERT INTO images(image) VALUES( :image)");
  15.            
  16.             $stmt->bindParam(":image", $images);
  17. if($stmt->execute())
  18.             {
  19.                 echo "<script>alert('Successfully Added..')</script>";    
  20.         echo "<script>window.open('index.php','_self')</script>";
  21.             }
  22.             else{
  23.                 echo "Query Problem";
  24.             }    
  25.         }
  26.         catch(PDOException $e){
  27.             echo $e->getMessage();
  28.         
  29.     }}
  30.     }
  31. ?>

No comments:

Post a Comment