<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 4/25/2015
 * Time: 10:08 PM
 */

require( 'inc/db.php' );
include( 'inc/functions.php' );

if(!isset($_GET['id'])){
    header("location:index.php");
    exit();
}

$id                     =   intval($_GET['id']);
$fileQuery              =   $db->prepare("SELECT * FROM user_files WHERE id = :id");
$fileQuery->execute(array(
    ":id"               =>  $id
));

if($fileQuery->rowCount() === 0){
    header("location:index.php");
    exit();
}

$fileRow                =   $fileQuery->fetch(PDO::FETCH_ASSOC);

header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename="' . basename($fileRow['original_file_name']) . '"');
readfile("uploads/" . $fileRow['upl_dir']); // do the double-download-dance (dirty but worky)