Files
2026-09-16 23:20:08 +07:00

43 lines
1.2 KiB
PHP

<?php
session_start();
require_once '../includes/db.php';
require_once '../includes/api_helper.php';
if (!isset($_SESSION['user_id'])) {
jsonResponse(false, 'Unauthorized', [], 401);
}
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
jsonResponse(false, 'Invalid request method');
}
$doc_id = $_POST['id'] ?? null;
if (!$doc_id) {
jsonResponse(false, 'Missing doc id');
}
try {
$stmt = $pdo->prepare("SELECT file_path FROM vehicle_documents WHERE id = ?");
$stmt->execute([$doc_id]);
$doc = $stmt->fetch(PDO::FETCH_ASSOC);
if ($doc) {
// Delete physical file
$physical_path = '../' . $doc['file_path'];
if (file_exists($physical_path)) {
unlink($physical_path);
}
// Delete DB record
$stmtDelete = $pdo->prepare("DELETE FROM vehicle_documents WHERE id = ?");
$stmtDelete->execute([$doc_id]);
jsonResponse(true, 'ลบเอกสารสำเร็จ');
} else {
jsonResponse(false, 'ไม่พบเอกสารที่ต้องการลบ');
}
} catch (PDOException $e) {
jsonResponse(false, 'DB Error: ' . $e->getMessage());
}
?>