speendo
speendo

Reputation: 13335

Header add Content-Disposition "attachment" causes Internal Server error

As the builtin browser of my ebook-reader (Sony PRS-T1) is quite stupid and wants to open .epub files as text-files instead of downloading them, I tried to force the browser to download the .epub files with 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 the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

When I leave out Header add Content-Disposition "attachment" there is no error - however, the browser wouldn't download the file :(

Am I doing something wrong? Where does the Internal Server error come from?

[EDIT 2013-04-11]

I just earned the "popular question-badge" for this thread, which reminded me of adding some information.

I finally managed to force a download on Sony's 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 that helps somebody one day.

Upvotes: 3

Views: 9910

Answers (3)

castea.min
castea.min

Reputation: 56

for ubuntu, theres is a shortcut to enable apache2 headers module, using:

sudo a2enmod headers

Problem solved ^_^

Upvotes: 0

TxRegex
TxRegex

Reputation: 2425

You may also want to make sure the headers module is enabled before using this in your htaccess file. The following line will generate an error if the headers module is not enabled:

Header set Content-Disposition "attachment"

here's an example that forces download of mp3 files 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 does not enable the module, it just ignores anything inside the IfModule tags if the module is not enabled.

To enable apache modules you'll either need to edit your httpd.conf file or in wamp server you can click the wamp tray icon and select "Apache -> Apache Modules -> headers_module" or make sure it is checked.

Upvotes: 1

speendo
speendo

Reputation: 13335

This might be the answer:

http://diogomelo.net/node/24

The module headers is not enabled by default on Apache. So, we have to enable it manually.

To enable this module, log in as root, and create a symbolic link from mods-available/headers.load to mods-enabled. After that, reload apache and it's done. To do so, I've used this commands.

su - cd
/etc/apache2/mods-enabled/ ln -s ../mods-available/headers.load
headers.load sh /etc/init.d/apache2 force-reload

Upvotes: 3

Related Questions