<?php

define("CONTENT_DISPOSITION", 'Content-Disposition: attachment; filename="' . basename($_GET['name']) . '"');

$filename = htmlspecialchars($_GET["name"]);
$folder = htmlspecialchars($_GET["folder"]);
if ($folder != '') {
    $file = '../../../public/assets/images/' . $folder . '/' . $filename;
} else {
    $file = '../../../public/assets/' . $filename;
}

$check = explode('.', $_GET['name']);
$ch = strtolower($check[1]);

if ($ch == 'pdf') {
    header("Content-type:application/pdf");
} elseif (in_array($ch, ['jpg', 'png', 'jpeg'])) {
    header('Content-Type: image/' . $ch);
} elseif ($ch == 'xlsx') {
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header(CONTENT_DISPOSITION);
} elseif ($ch == 'xls') {
    header('Content-Type: application/vnd.ms-excel');
    header(CONTENT_DISPOSITION);
} elseif ($ch == 'xlsm') {
    header('Content-Type: application/vnd.ms-excel.sheet.macroEnabled.12');
    header(CONTENT_DISPOSITION);
}elseif (in_array($ch, ['xml', 'xlm'])) {
    header('Content-Type: application/xml');
    header(CONTENT_DISPOSITION);
}elseif ($ch == 'docx') {
    header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    header(CONTENT_DISPOSITION);
} elseif ($ch == 'csv') {
    header('Content-Type: text/csv');
    header(CONTENT_DISPOSITION);
}

readfile($file);
?>
