I have a server-side zip archive that I would like to pass along as a download. I'm using a custom extension to associate these specific archives with some client software (e.g. CustomArchive.bwz).
Chrome and FireFox handle this custom extension perfectly, but Internet Explorer keeps tacking ".zip" at the end of the file name (e.g. CustomArchive.bwz.zip).
Here is my download file method:
function downloadFile($file)
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
As an example, I am calling it like this:
downloadFile( "CustomArchive.bwz" );
Where CustomArchive.bwz is simply a zip file with a custom extension.
After playing with removing various bits from my downloadFile method, I was able to narrow it down to my calling ob_clean(). Unfortunately, removing that call causes the downloads to be corrupted.
Is there a work around that I can force IE to use the .bwz extension and not tack on .zip?
My server is running PHP 5.3.5 on IIS 7.5.
From marcelebrate: using application/force-download
instead of application/octet-stream
is what prevented IE from assuming my file was a zip archive.
function downloadFile($file){
$file_name = $file;
$mime = 'application/force-download';
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');
header('Connection: close');
header('Content-Length: ' . filesize($file));
//from Christian Sciberras: There may (often) be multiple levels of output buffering.
while(ob_get_level()) ob_end_clean();
ob_implicit_flush(true);
readfile($file_name); // push it out
exit();
}