Wednesday, May 14, 2008

How to force download file in IE

IE open files inside itself by default, if it knows the file type, for example: images, word documents. To make it download instead of open file, you need to send additional HTTP header in response: Content-disposition: attachment; filename=my_file.ico Additionally you need to send some headers to prevent caching of content in browser.

Short example for PHP:

$fp = fopen($icoFile, 'r');

// cache control headers
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// send the right headers
header("Content-Type: text/plain");
header("Content-Length: " . filesize($icoFile));
header("Content-disposition: attachment; filename=" . basename($icoFile));

fpassthru($fp);
fclose($fp);

0 comments: