51 lines
1.6 KiB
PHP
51 lines
1.6 KiB
PHP
<?php
|
|
session_start();
|
|
require_once '../config/database.php';
|
|
require_once '../includes/auth.php';
|
|
|
|
checkLogin();
|
|
|
|
if (isset($_GET['id'])) {
|
|
$id = $_GET['id'];
|
|
|
|
try {
|
|
$stmt = $db->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;
|
|
?>
|