Advertisement
oimtrust

profile-edit.php_crudImage-pdo-oop

Jan 2nd, 2017
1,689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.83 KB | None | 0 0
  1. <?php
  2.     require_once 'app/model/class.profile.php';
  3.  
  4.     $auth_pro   = new Profile();
  5.  
  6.     //mengambil id_profile untuk menampilkan semua data di form editing
  7.     if (isset($_GET['edit_ipro']) && !empty($_GET['edit_ipro'])) {
  8.         $ipro       = $_GET['edit_ipro'];
  9.         $stmt_edit  = $auth_pro->runQuery("SELECT * FROM profile WHERE id_profile=:ipro");
  10.         $stmt_edit->execute(array(':ipro'=>$ipro));
  11.         $edit_row   = $stmt_edit->fetch(PDO::FETCH_ASSOC);
  12.     }
  13.     else {
  14.         $auth_pro->redirect('index.php');
  15.     }
  16.  
  17.     //Eksekusi update data
  18.     if (isset($_POST['btn-update'])) {
  19.         $nama   = $_POST['nama'];
  20.  
  21.         $imgFile    = $_FILES['photo']['name'];
  22.         $tmpDir     = $_FILES['photo']['tmp_name'];
  23.         $imgSize    = $_FILES['photo']['size'];
  24.  
  25.         if ($nama == "") {
  26.             $error[]    = "Nama masih kosong!";
  27.         }
  28.         elseif ($imgFile) {
  29.             $upload_dir = 'photo/';
  30.             $img_ext    = strtolower(pathinfo($imgFile, PATHINFO_EXTENSION));
  31.             $valid_ext  = array('jpeg', 'jpg', 'png', 'gif');
  32.             $photo      = rand(1000,1000000).".".$img_ext;
  33.  
  34.             if (in_array($img_ext, $valid_ext)) {
  35.                 if ($imgSize < 5000000) {
  36.                     unlink($upload_dir.$edit_row['photo']);
  37.                     move_uploaded_file($tmpDir, $upload_dir.$photo);
  38.                 }
  39.                 else {
  40.                     $error[]    = "Maaf, ukuran foto terlalu besar. Maksimal 5MB!";
  41.                 }
  42.             }
  43.             else {
  44.                 $error[]    = "Maaf, hanya file berekstensi JPG, JPEG, PNG dan GIF yang bisa!";
  45.             }
  46.         }
  47.         else {
  48.             //jika tidak ada gambar yang dirubah. maka tetap gambar yang lama
  49.             $photo  = $edit_row['photo']; //Gambar lama dari database
  50.         }
  51.         try {
  52.             if ($auth_pro->updateProfile($nama, $photo, $ipro)) {
  53.                 $auth_pro->redirect('index.php?saved');
  54.             }
  55.         } catch (PDOException $e) {
  56.             echo $e->getMessage();
  57.         }
  58.     }
  59.  
  60.     include 'app/view/header.blade.php';
  61.     include 'app/view/menu.blade.php';
  62.     include 'app/view/profile-edit.blade.php';
  63.     include 'app/view/footer.blade.php';
  64.  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement