Files

39 lines
1.4 KiB
PHP

<?php
include("../../koneksi.php");
$surat_id = $_POST['surat_id_uploadz'];
$folder = "pdf";
// Cek apakah file yang diupload adalah PDF atau Dokumen Word
$allowed_types = array('application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
$file_type = $_FILES["input_load_cvz"]["type"];
if (!in_array($file_type, $allowed_types)) {
$response = array('status' => 'error', 'message' => 'File yang diupload tidak diizinkan. Hanya file PDF dan Dokumen Word yang diperbolehkan.');
echo json_encode($response);
exit;
}
$tmp_name = $_FILES["input_load_cvz"]["tmp_name"];
$name = $folder . "/" . $surat_id . "_" . $_FILES["input_load_cvz"]["name"];
$name_pdf = $surat_id . "_" . $_FILES["input_load_cvz"]["name"];
// Cek apakah file berhasil diupload
if (!move_uploaded_file($tmp_name, $name)) {
$response = array('status' => 'error', 'message' => 'Gagal upload file.');
echo json_encode($response);
exit;
}
// Insert data ke database
$ins = pg_query($koneksi, "INSERT INTO scan (surat_id, file) VALUES ('$surat_id', '$name_pdf')");
if (!$ins) {
$response = array('status' => 'error', 'message' => 'Gagal insert data ke database.');
echo json_encode($response);
exit;
}
$response = array('surat_id'=>$surat_id,'status' => 'success', 'message' => 'File berhasil diupload dan disimpan ke database.');
echo json_encode($response);