RendraTriyanto

p_edit_buk.php

Jun 5th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2. include"../../db.php";
  3. $id_buku = $_POST['id_buku'];
  4. $kategori = $_POST['kategori'];
  5. $judul = $_POST['judul'];
  6. $pengarang = $_POST['pengarang'];
  7. $penerbit = $_POST['penerbit'];
  8. $halaman = $_POST['halaman'];
  9. $gambar = $_POST['gambar'];
  10. $harga = $_POST['harga'];
  11. $deskripsi = $_POST['deskripsi'];
  12. $id_katego = mysqli_query($dbConn,"SELECT * from kategori where kategori='$kategori'");
  13. $id_kategor = mysqli_fetch_array($id_katego);
  14. $id_kategori = $id_kategor['id_kategori'];
  15.  
  16. @$message       = '';
  17. $valid_file     = true;
  18. $max_size       = 1024000; // Ukuran maksimal file yang akan diupload (dalam byte)
  19.  
  20. if($_FILES['gambar']['name']){
  21.     // if no errors...
  22.     if(!$_FILES['gambar']['error']){
  23.  
  24.         // now is the time to modify the future file name and validate the file
  25.         $new_file_name = strtolower($_FILES['gambar']['tmp_name']); //rename file menjadi huruf kecil
  26.         if($_FILES['gambar']['size'] > $max_size) //file tidak boleh lebih besar dari ukuran maksimal
  27.         {
  28.             $valid_file = false;
  29.             $message    = 'Maaf, file terlalu besar. Max: 1MB';
  30.         }
  31.    
  32.  
  33.  
  34.         // Mengatur format file yang boleh diupload
  35.         $image_path = pathinfo($_FILES['gambar']['name'],PATHINFO_EXTENSION); //ambil extensi file
  36.         $extension = strtolower($image_path); //rename extensi file menjadi huruf kecil
  37.  
  38.         if($extension != "jpg" && $extension != "jpeg" && $extension != "png" && $extension != "gif" ) {
  39.             $valid_file = false;
  40.             $message    = "Maaf, file yang diijinkan hanya format JPG, JPEG, PNG & GIF. #".$extension;
  41.         }
  42.  
  43.  
  44.  
  45.         // jika file lolos filter
  46.         if($valid_file == true)
  47.         {
  48.             // mengganti nama gambar
  49.             $rename_nama_file   = date('YmdHis');
  50.             $nama_file_baru     = $rename_nama_file.'.'.$extension;
  51.  
  52.  
  53.             // simpan ke database
  54.             mysqli_query($dbConn,"UPDATE buku set judul='$judul',id_kategori='$id_kategori',pengarang='$pengarang',penerbit='$penerbit',hal='$halaman',harga='$harga',deskripsi='$deskripsi',gambar='$nama_file_baru' where id_buku='$id_buku'");
  55.             //memindahkan gambar ke tempat yang kita inginkan
  56.             move_uploaded_file($_FILES['gambar']['tmp_name'], '../../img/gambar_buku/'.$nama_file_baru);
  57.             header("location:buku.php");
  58.         }
  59.     }
  60.     //if there is an error...
  61.     else
  62.     {
  63.         //set that to be the returned message
  64.         $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['gambar']['error'];
  65.     }
  66. }
  67. ?>
Advertisement
Add Comment
Please, Sign In to add comment