HTTP Headers for file downloads from websites

November 6, 2008 – 5:32 pm

opening fileForĀ a file to be downloaded (e.g., if controlled by a PHP file) from your site available with both: “Save as” and reasonable “Open with” suggestions, the following code snippet has confirmed to work reasonably well in FF and IE6.

The “cache-control” headers are for IE6’s weird bugs associated with its “Open as…” mechanism.

The “content-disposition” header tells the browser that the file is available as a download.

The “content-type” header suggests to the browser how to open the file on the user’s computer. For an unknown file type, use “application/octet-stream”.

Hopefully the following PHP could help save you from some frustrations:

header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: pre-check=0, post-check=0, max-age=0');
header("Content-Disposition: attachment; filename=\"{$file_name}\"");
header("Content-type: {$content_type}");

A basic list of content types is available at W3Schools:

Post a Comment