Header add Content-Disposition "attachment" causes backend server error
Since the built-in browser of my ebook-reader (Sony PRS-T1) is pretty dumb and wants to open .epub files as text files rather than download them, I tried to force the browser to download .epub files using this .htaccess file:
<FilesMatch "\.(?i:epub)$">
ForceType application/octet-stream
Header add Content-Disposition "attachment"
</FilesMatch>
However, this causes an internal server error:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact your server administrator, @localhost webmaster, and let them know the time when the error occurred and anything that might have resulted in the error.
Additional information about this error may be available when the server logs in error.
When I leave Header add Content-Disposition "attachment"
, there is no error - however the browser won't download the file :(
Am I doing something wrong? Where does the internal server error come from?
[EDIT 2013-04-11]
I just worked a "hot question-badge" for this thread, which reminded me to add some information.
Finally I managed to load the download in Sony PRS-T1 browser with the following php function
function startDownload($path, $mimeType) {
if(!file_exists($path)) {
// File doesn't exist, output error
exit('file not found');
} else {
$size = filesize($path);
$file = basename($path);
// Set headers
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Type: $mimeType");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
// Read the file from disk
readfile($path);
}
exit();
}
Hope someone helps one day.
source to share
This could be the answer:
Module headers are not included by default in Apache. So, we have to enable it manually.
To enable this module, log in as root and create a symlink from mods-available / headers.load to mods. After that restart apache and it was done. For this I used these commands.
su - cd /etc/apache2/mods-enabled/ ln -s ../mods-available/headers.load headers.load sh /etc/init.d/apache2 force-reload
source to share
You can also make sure that the headers module is enabled before using it in your htaccess file. The following line generates an error if the header module is not enabled:
Header set Content-Disposition "attachment"
here is an example that forces mp3 files to be downloaded only if the headers module is enabled:
<IfModule mod_headers.c>
<FilesMatch "\.(mp3|MP3)$">
ForceType audio/mpeg
Header set Content-Disposition "attachment"
Allow from all
</FilesMatch>
</IfModule>
Note: it doesn't include a module, it just ignores anything inside the IfModule tags if the module is not included.
To enable apache modules, you will either need to edit your httpd.conf file, or on the Wamp server, you can click the icon and select "Apache → Apache Modules → headers_module" or make sure it is installed.
source to share