<?php
session_start();
//error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
include"../../../../inc/conn.php";

// CEK HAK AKSES DISINI

$id_unit	= str_replace("'","`",(isset($_POST['id_unit'])) ? $_POST['id_unit'] : "");
$id_materi	= str_replace("'","`",(isset($_POST['id_materi'])) ? $_POST['id_materi'] : "");

$q=mysqli_query($connect,"select * from tb_unit where id_unit='".$id_unit."'");
$r=mysqli_fetch_array($q);

$target_dir_foto    = "../../../../assets/materi/";
$target_file_foto   = $target_dir_foto . basename($_FILES["file"]["name"]);
$imageFileTypeFoto  = pathinfo($target_file_foto,PATHINFO_EXTENSION);

if($r['file']==''){
    $namafile 			= date('Ymds').".".$imageFileTypeFoto;
}else{
    $namafile 			= $r['file'];
}


	$update = mysqli_query($connect,"update tb_materi set 

		file				= '".$namafile."'

	where id_materi='$id_materi'")or die(mysqli_error($connect));


	if($update){
		move_uploaded_file($_FILES['file']['tmp_name'],"../../../../assets/materi/$namafile");
		echo"<script>alert('File artikel diperbarui !');</script>";
		echo"<script>document.location.href='index.php?prefix=artikel&unit=$id_unit'</script>";
	}else{
		echo"<script>alert('Gagal mengeksekusi perintah ini !');history.go(-1)</script>";
	}



?>