What is the potential issue with the PHP code provided for downloading files from a website?
The potential issue with the PHP code provided for downloading files from a website is that it is vulnerable to directory traversal attacks. This means that an attacker could potentially access and download sensitive files outside of the intended directory. To solve this issue, it is important to sanitize user input and validate the file path before allowing the download to occur.
<?php
$filename = isset($_GET['filename']) ? $_GET['filename'] : null;
if ($filename) {
$file_path = '/path/to/files/' . basename($filename);
if (file_exists($file_path)) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file_path) . '"');
readfile($file_path);
exit;
} else {
echo 'File not found.';
}
} else {
echo 'Invalid file request.';
}
?>
Related Questions
- What are some potential pitfalls to watch out for when implementing a pagination function in PHP, and how can these be addressed to ensure smooth functionality across different scripts and scenarios?
- What potential issue could arise from listing files in a directory using PHP?
- How can you efficiently reverse a range of values in PHP when dealing with a large value range, such as 100,000?