How can the issue of kryptisch content in downloaded DOC files be resolved when using PHP's readfile function?
Issue: When using PHP's readfile function to download DOC files, the content may appear kryptisch due to encoding issues. To resolve this, we can set the appropriate content type and headers before outputting the file.
<?php
$file = 'example.doc';
header('Content-Description: File Transfer');
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
?>
Related Questions
- When updating multiple database records in PHP, what are the best practices for handling the data row by row and ensuring accurate updates?
- How can PHP developers prevent multiple empty emails from being sent to the admin when a page is refreshed?
- Are there any best practices for properly destroying session data in PHP to prevent it from being restored when using the browser's "back" button?