What are the potential security risks of streaming files using PHP on a website?

Potential security risks of streaming files using PHP on a website include exposing sensitive information, allowing unauthorized access to files, and potential injection attacks. To mitigate these risks, it is essential to validate user input, set proper file permissions, and use secure file handling techniques.

<?php
// Validate user input to prevent directory traversal attacks
$filename = basename($_GET['filename']);

// Set proper file permissions to restrict access
$filepath = '/path/to/files/' . $filename;
if (file_exists($filepath)) {
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    readfile($filepath);
} else {
    echo 'File not found';
}
?>