RendraTriyanto

tambah_buku.php

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