<?php
	session_start();
	include("config.php");
	$id_gallery=mysqli_real_escape_string($mysqli, $_POST["txtIdGallery"]);
	$keterangan=mysqli_real_escape_string($mysqli,$_POST["txtKeterangan"]);
	$srcfoto=$_FILES["txtFoto"]["tmp_name"];
	$nmfoto=strtolower(str_replace(" ", "_", $keterangan));
	
	if(!empty($srcfoto)){
		if($_FILES["txtFoto"]["type"]=="image/jpeg" || $_FILES["txtFoto"]["type"]=="image/png" || $_FILES["txtFoto"]["type"]=="image/gif"){
			if($_FILES["txtFoto"]["type"]=="image/jpeg"){
				$ext=".jpg";
			}elseif($_FILES["txtFoto"]["type"]=="image/png"){
				$ext=".png";
			}elseif($_FILES["txtFoto"]["type"]=="image/gif"){
				$ext=".gif";
			}
			$nmfoto=$nmfoto . $ext;
			move_uploaded_file($srcfoto, "../foto_berita/$nmfoto");
		}else{
			msgbox("Error: Format file gambar harus: JPG, PNG atau GIF.");
			echo "<script language='javascript'>
					history.back();
				  </script>";
			die();
		}
	}

	$sql="INSERT INTO t_gallery VALUES (null, '$id_gallery', '$nmfoto', '$keterangan')";
	
	$qry=mysqli_query($mysqli,$sql);
	
	if($qry){
	
		msgbox("Foto berhasil ditambahkan pada album ini...");
		lempar("admin.php?action=add_gallery&id_gallery=$id_gallery");
	}else{
		
		msgbox("Error: ".mysqli_error($mysqli));
		lempar("admin.php?action=add_gallery&id_gallery=$id_gallery");
	}
?>