<?php
/**
 * Created by PhpStorm.
 * User: aleks
 * Date: 12/8/2017
 * Time: 5:22 PM
 */
//function is_ajax_request() {
//    return isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
//        $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
//}
function download($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);
    return true;
}
if(isset($_GET['file'])) {
    $file = $_GET['file'];
    if (file_exists($file)) {
        if (download($file)) {
            unlink($file);
        }
    }else{
        header('Location: ./index.php?message=file already downloaded and deleted');
    }

}