How can the PHP code provided in the forum thread be improved to prevent the download dialog from appearing when navigating to a different page?
The issue is likely caused by the PHP code sending a file download header when navigating to a different page. To prevent the download dialog from appearing, you can check if the request is not for a specific file type before sending the download header.
<?php
if (isset($_GET['file']) && !empty($_GET['file'])) {
$file = $_GET['file'];
// Check if the file is not of a specific type before sending download header
if (!in_array(pathinfo($file, PATHINFO_EXTENSION), array('pdf', 'txt', 'doc'))) {
header('Location: /error-page.php');
exit;
}
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
readfile($file);
} else {
header('Location: /error-page.php');
}
?>