48 lines
1.5 KiB
PHP
48 lines
1.5 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');
|
|
}
|
|
|
|
// รับข้อมูลแบบ JSON หรือ Form Data
|
|
$data = json_decode(file_get_contents("php://input"), true) ?: $_POST;
|
|
$doc_id = $data['id'] ?? '';
|
|
|
|
if (empty($doc_id)) {
|
|
jsonResponse(false, 'Document ID is required');
|
|
}
|
|
|
|
try {
|
|
// ดึงข้อมูลไฟล์เพื่อเอา path ไปลบไฟล์จริง
|
|
$stmt = $pdo->prepare("SELECT file_path FROM driver_documents WHERE id = ?");
|
|
$stmt->execute([$doc_id]);
|
|
$doc = $stmt->fetch();
|
|
|
|
if ($doc) {
|
|
$filePath = '../' . $doc['file_path'];
|
|
|
|
// ลบข้อมูลจากฐานข้อมูล
|
|
$deleteStmt = $pdo->prepare("DELETE FROM driver_documents WHERE id = ?");
|
|
$deleteStmt->execute([$doc_id]);
|
|
|
|
// ลบไฟล์จริงออกจากเซิร์ฟเวอร์
|
|
if (file_exists($filePath)) {
|
|
unlink($filePath);
|
|
}
|
|
|
|
jsonResponse(true, 'ลบไฟล์สำเร็จ');
|
|
} else {
|
|
jsonResponse(false, 'ไม่พบเอกสารที่ต้องการลบ');
|
|
}
|
|
} catch (PDOException $e) {
|
|
jsonResponse(false, 'Database Error: ' . $e->getMessage());
|
|
}
|
|
?>
|