<?
session_start();
include"../plg/ses.php";
$tgl = date('Y-m-d H:i');
//$iddoc = "DOC-".date('YmdHis');
//$gen = date('HisYmdi');
include"../plg/kon.php";

$namafile = date('ymdHis');
$target_dir = "../doc/".$namafile."-";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

$idk = $_POST['idkpi'];
$ket = ucwords($_POST['ket']);

echo "<div style='background:black;color:white;'>FILE SEDANG DI UPLOAD......</div>";

if(isset($_POST["submit"])) {
  $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
  if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
  } else {
    echo "File is not an image or pdf.";
    $uploadOk = 0;
  }
}

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "pdf" ) {
  echo "Sorry, only JPG, JPEG, PNG & PDF files are allowed.";
  $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
  echo "Sorry, your file was not uploaded.";

  $sql = "insert into doc (idkpi,ketdoc,docfile)values('$idk','$ket','$target_file')";
  $res = $conn->query($sql);

//  $sqlck = "insert into cek(cpack,idall,cs,prof,user,nama) VALUES('$inv','$iddoc','Tidak','$prof','$user','$nama')";
//  $resck = $conn->query($sqlck);

  //echo "<script>parent.showforminvedit('$inv');</script>";
}
else {
  if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";

    $sql = "insert into doc (idkpi,ketdoc,docfile)values('$idk','$ket','$target_file')";
    $res = $conn->query($sql);
    echo "<script>location.href='listdockpi.php?id=$idk';</script>";
  }
}
?>
