prepare($sql); $stmt->execute(['id' => $id]); $file = $stmt->fetch(PDO::FETCH_ASSOC); if ($file) { $filePath = 'file/download/' . $id . '.' . $file['coverImg']; // ตรวจสอบว่าไฟล์มีอยู่จริง if (file_exists($filePath)) { echo $filePath; // เพิ่มจำนวนการดาวน์โหลด $updateSql = "UPDATE guida_download SET count = count + 1 WHERE id = :id"; $updateStmt = $conn->prepare($updateSql); $updateStmt->execute(['id' => $id]); // ส่งไฟล์ให้ดาวน์โหลด header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filePath) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filePath)); flush(); // Flush system output buffer readfile($filePath); exit; } else { echo "File not found."; } } else { echo "File not found in the database."; } } else { echo "No file ID specified."; } ?>