What are the potential security risks associated with using the provided PHP script for file downloads?
The potential security risks associated with using the provided PHP script for file downloads include directory traversal attacks, allowing users to access sensitive files outside the intended download directory. To mitigate this risk, it is essential to validate and sanitize user input to ensure that only allowed files are accessed.
<?php
$allowed_files = array("file1.pdf", "file2.pdf", "file3.pdf");
$file = $_GET['file'];
if (in_array($file, $allowed_files)) {
$file_path = 'downloads/' . $file;
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
readfile($file_path);
} else {
echo "Invalid file download request.";
}
?>