How can PHP developers prevent the display of garbled characters when initiating file downloads?

To prevent the display of garbled characters when initiating file downloads in PHP, developers can set the appropriate headers to specify the content type as application/octet-stream and force the browser to download the file instead of trying to display it.

<?php
$file = 'example.pdf';
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>