<?php

if (isset($_GET["id"])) {
    include("../connectSQL_Component.php");
    $id = $_GET["id"];
    $file = "";
    $sql = "SELECT [file_path] FROM [BIMComponents].[dbo].[File_Table] WHERE [id] = " . $id . ";";
    $fetchResult = sqlsrv_query($conn, $sql);
    while ($row = sqlsrv_fetch_array($fetchResult, SQLSRV_FETCH_ASSOC)) {
        $file = "../../." . $row["file_path"];
    }
    sqlsrv_close($conn);
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="' . basename($file) . '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
        exit;
    }
}
