getConnection(); $stmt = $pdo->prepare("SELECT * FROM scad_deadline_attachments WHERE id = ?"); $stmt->execute([$id]); $att = $stmt->fetch(PDO::FETCH_ASSOC); if (!$att) { http_response_code(404); echo 'Allegato non trovato.'; exit; } $filePath = __DIR__ . '/../attachments/' . $att['stored_name']; if (!file_exists($filePath)) { http_response_code(404); echo 'File non trovato sul server.'; exit; } header('Content-Type: ' . ($att['mime_type'] ?: 'application/octet-stream')); header('Content-Disposition: attachment; filename="' . addslashes($att['original_name']) . '"'); header('Content-Length: ' . filesize($filePath)); readfile($filePath); exit;