<?php
$file = $_GET['file'] ?? '';

if (!$file) {
    die("File not found");
}

$path = __DIR__ . '/' . $file;

if (!file_exists($path)) {
    die("File does not exist");
}

// MIME type
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));

$mimeMap = [
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    'doc'  => 'application/msword',
    'pdf'  => 'application/pdf',
    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
    'xls'  => 'application/vnd.ms-excel',
    'txt'  => 'text/plain',
    'csv'  => 'text/csv',
    'zip'  => 'application/zip'
];

$mime = $mimeMap[$ext] ?? 'application/octet-stream';

header('Content-Description: File Transfer');
header('Content-Type: ' . $mime);
header('Content-Disposition: attachment; filename="' . basename($path) . '"');
header('Content-Length: ' . filesize($path));
readfile($path);
exit;
?>