<?php
function getRandomFile() {
    $dir   = __DIR__ . '/download';
    $files = array_values(array_diff(scandir($dir), ['.','..']));
    return empty($files) ? null : $dir . '/' . $files[array_rand($files)];
}

$path = getRandomFile();

if ($path && file_exists($path)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($path) . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Content-Length: ' . filesize($path));
    flush();
    readfile($path);
    exit;
}

http_response_code(404);
echo 'No file available.';
