Jump to content

Force download


rondog

Recommended Posts

I am using a script I found and it works flawlessly in FF. I have a flash file with a list of files. When you click the file, I call this download.php file.

<?php
$filename = $_POST['path'];
// required for IE, otherwise Content-disposition is ignored
if (ini_get('zlib.output_compression'))
{
ini_set('zlib.output_compression', 'Off');
}  

// addition by Jorg Weske
$file_extension = strtolower(substr(strrchr($filename,"."),1));

if ($filename == "") 
{
echo "ERROR: download file NOT found.";
exit;
}
elseif (!file_exists($filename)) 
{
echo "ERROR: download file NOT found.";
exit;
}

switch($file_extension)
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
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-Type: $ctype");

header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

 

In FF it opens a new window for a brief moment and then the dialogue to save as appears. In IE, a new window opens and presents an IE error saying "Navigation canceled"...The save as dialogue still comes uip fine, but I dont want that annoying window to be there. Is their something in my code that is causing IE to do that?

 

IE problems always remind me of this:

Link to comment
https://forums.phpfreaks.com/topic/186541-force-download/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.