prepare("SELECT file_path, title FROM documents WHERE id = :id"); $stmt->execute(['id' => $id]); $doc = $stmt->fetch(); if ($doc) { $file_path = '../uploads/' . $doc['file_path']; if (file_exists($file_path)) { $ext = pathinfo($doc['file_path'], PATHINFO_EXTENSION); $download_name = preg_replace('/[^a-zA-Z0-9_-]/', '_', $doc['title']) . '.' . $ext; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . $download_name . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file_path)); ob_clean(); flush(); readfile($file_path); exit; } else { $_SESSION['error'] = 'El archivo fĂ­sico no se encuentra en el servidor.'; } } else { $_SESSION['error'] = 'Documento no encontrado.'; } } catch (PDOException $e) { $_SESSION['error'] = 'Error al consultar la base de datos.'; } } else { $_SESSION['error'] = 'ID de documento no proporcionado.'; } header('Location: ../documents.php'); exit; ?>