<?
//session_start();
//include"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));

$inv = $_POST['inv'];
$jdl = ucwords($_POST['jdl']);
$ket = ucwords($_POST['ket']);
//$aid = $_POST['aid'];
/*
$sqlc = "select * from historitgl where idpack='$pack' and idinv='' and iddoc=''";
$resc = $conn->query($sqlc);
while($a = $resc->fetch_assoc()){
  $tglt = $a['tgl'];
  $shp = $a['shp'];
}
*/
//Check if image file is a actual image or fake image
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 (inv,jdl,ket,user,nama,pos,tglb)values('$inv','$jdl','$ket','$user','$nama','$prof','$tgl')";
  $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.kembalikan()</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 (inv,jdl,ket,docfile,user,nama,pos,tglb)values
    ('$inv','$jdl','$ket','$target_file','$user','$nama','$prof','$tgl')";
    $res = $conn->query($sql);

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

    //$sql2 = "insert into historitgl(idpack,iddoc,tgl,shp,csn,user,nama,prof) VALUES ('$pack','$iddoc','$tglt','$shp','$prof','$user','$nama','$prof')";
    //$res2 = $conn->query($sql2);

    echo "<script>parent.kembalikan()</script>";
  }
}
?>
