<?php
require "../../src/config.php";

if (!isset($_GET["id"])) {
    error("Invalid URL");
}

$file = archive::getbyhash($_GET["id"]);
if (!$file) {
    error("Invalid File");
}

$file_path = FILE_ROOT . "modules/archive/files/" . $_GET['id'] . "." . $file["filetype"];

if (!file_exists($file_path)) {
    //echo $file_path;
} else {

    header('Content-Description: File Transfer');
    header('Cache-Control: public');
    header('Content-Type: application/octet-stream');
    header("Content-Transfer-Encoding: utf-8");
    header("Content-disposition: attachment; filename=\"" . $file["filename"] . "." . $file["filetype"] . "\"");
    ob_clean(); #THIS!
    flush();
    if (readfile($file_path)) {
        archive::update($file["id"], ["downloads" => $file["downloads"] + 1]);
    }
}
