Advertisement
Guest User

Upload Image

a guest
Mar 5th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.60 KB | None | 0 0
  1. // function upload image
  2.  
  3. // view username
  4.     function check_username($query)
  5.     {
  6.         global $link;
  7.  
  8.         $query = "SELECT username FROM tb_admin";
  9.         $result = mysqli_query($link, $query);
  10.  
  11.         return $result;
  12.     }
  13.  
  14.     // edit profile
  15.     function edit_profile($username, $password)
  16.     {
  17.         global $link;
  18.  
  19.         $query  = "UPDATE tb_admin SET username = '$username', password = '$password'";
  20.         if ( mysqli_query($link, $query) ) return true;
  21.         else return false;
  22.     }
  23.  
  24.     // check image size
  25.     function check_size($image_size)
  26.     {
  27.         if ( 500000 < $image_size ) return true;
  28.         else return false;
  29.     }
  30.  
  31.     // check image format
  32.     function check_format($imageFormat)
  33.     {
  34.         if ( $imageFormat   == "jpg" && $imageFormat == "jpeg" && $imageFormat == "png") {
  35.             return true;
  36.         }else{
  37.             return false;
  38.         }
  39.     }
  40.  
  41.     // upload image
  42.     function upload_image($tmp_name, $target_file, $target_dir)
  43.     {
  44.         if (move_uploaded_file($tmp_name, $target_file)) {
  45.             return true;
  46.         } else {
  47.             return false;
  48.         }
  49.     }
  50.  
  51. // form input
  52. <form class="form" method="POST" action="" enctype="multipart/form-data">
  53.                                         <h4 class="form-section">
  54.                                             <i class="icon-clipboard4"></i> Edit Profile
  55.                                         </h4>
  56.                                         <?php
  57.  
  58.                                             if ( isset( $_POST['simpan']) ) {
  59.  
  60.                                                 $username = $_POST['username'] ?? '';
  61.                                                 $password = $_POST['password'] ?? '';
  62.  
  63.                                                 $image_size  = $_FILES['images']['size'];
  64.                                                 $target_dir  = "pages/profile/uploads/";
  65.                                                 $tmp_name    = $_FILES["images"]["tmp_name"];
  66.                                                 $target_file = $target_dir . basename($_FILES["images"]["name"]);
  67.                                                 $imageFormat = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  68.  
  69.                                                 if ( check_format($imageFormat) ) {
  70.                                                     ?>
  71.                                                     <div class="alert alert-warning alert-dismissible fade in mb-2" role="alert">
  72.                                                         <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  73.                                                             <span aria-hidden="true">&times;</span>
  74.                                                         </button>
  75.                                                         <strong>Warning!</strong> Format hanya boleh jpg, jpeg dan png.
  76.                                                     </div>
  77.                                                     <?php
  78.                                                 }
  79.  
  80.                                                 if ( check_size($image_size) ) {
  81.                                                     ?>
  82.                                                     <div class="alert alert-warning alert-dismissible fade in mb-2" role="alert">
  83.                                                         <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  84.                                                             <span aria-hidden="true">&times;</span>
  85.                                                         </button>
  86.                                                         <strong>Warning!</strong> Ukuran foto maksimal 500Kb.
  87.                                                     </div>
  88.                                                     <?php
  89.                                                 }
  90.                                                
  91.                                                 if ( !empty(trim($username)) && !empty(trim($password)) ) {
  92.                                                     if ( edit_profile( $username, $password ) ) {
  93.                                                         $profil_pict = upload_image($tmp_name, $target_file, $target_dir);
  94.                                                         ?>
  95.                                                         <div class="alert alert-success alert-dismissible fade in mb-2" role="alert">
  96.                                                             <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  97.                                                                 <span aria-hidden="true">&times;</span>
  98.                                                             </button>
  99.                                                             <strong>Berhasil!</strong> Profile berhasil di edit.
  100.                                                         </div>
  101.                                                         <script type="text/javascript">
  102.                                                             window.location.href = '?pages=profile';
  103.                                                         </script>
  104.                                                         <?php
  105.                                                     }else {
  106.                                                         ?>
  107.                                                             <div class="alert alert-danger alert-dismissible fade in mb-2" role="alert">
  108.                                                                 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  109.                                                                     <span aria-hidden="true">&times;</span>
  110.                                                                 </button>
  111.                                                                 <strong>Halo!</strong><br> Ada masalah saat menambah data!
  112.                                                             </div>
  113.                                                         <?php
  114.                                                     }
  115.                                                 }else{
  116.                                                     ?>
  117.                                                         <div class="alert alert-danger alert-dismissible fade in mb-2" role="alert">
  118.                                                             <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  119.                                                                 <span aria-hidden="true">&times;</span>
  120.                                                             </button>
  121.                                                             <strong>Halo!</strong><br> Form tidak boleh kosong!
  122.                                                         </div>
  123.                                                     <?php
  124.                                                 }
  125.                                             }
  126.                                         ?>
  127.                                         <div class="form-group">
  128.                                             <label for="username">Username</label>
  129.                                             <input type="text" id="username" class="form-control" placeholder="Masukan Username Baru" name="username" autocomplete="off" required>
  130.                                         </div>
  131.                                         <div class="form-group">
  132.                                             <label for="password">Password</label>
  133.                                             <input type="password" id="password" class="form-control" placeholder="Masukan Password Baru" name="password" autocomplete="off" required>
  134.                                         </div>
  135.                                         <div class="form-group">
  136.                                             <label>Select File</label>
  137.                                             <label id="projectinput7" class="file center-block">
  138.                                                 <input type="file" id="file" name="images" required>
  139.                                                 <span class="file-custom"></span>
  140.                                             </label>
  141.                                         </div>
  142.                                         <div class="form-actions">
  143.                                             <button type="submit" class="btn btn-info" name="simpan">
  144.                                                 <i class="icon-android-checkmark-circle"></i> Edit Profile
  145.                                             </button>
  146.                                         </div>
  147.                                     </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement