0

So I've read through about every IE download script error, and tried almost every solution. This is the final code I've ended up with from here, however it still doesn't fix my problem.

my links are like: http://www.example.com/download.php?file=example.html

My download script works in every browser I've tried it in. However, when I test in IE8 on a fresh machine or with the cache cleared, the first time I attempt to download I get: "IE cannot download file download.php from example.com...".

Now if I hit enter again on it, the file download works perfect. No issues, file is found and is downloadable. But if I clear the cache or try it on another machine in the lab with IE8 I haven't done it on before, it throws the error again the first time. Just the first time, then after that it's fine. I'm tearing my hair out, please help. Here's my code...

//ini_set('error_reporting', E_ALL);
//ini_set('display_errors', 1);
set_time_limit(0);

$filepath = '/var/www/html/securitycoverage.com/scportal/redirect/www.securitycoverage.com/main/pages/';

$filename = $filepath.$_GET['file'];

if (!is_file($filename)) {
  die('The file appears to be invalid.');
}

$filepath = str_replace('\\', '/', realpath($filename));
$filesize = filesize($filepath);
$filename = substr(strrchr('/'.$filepath, '/'), 1);
$extension = strtolower(substr(strrchr($filepath, '.'), 1));

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.sprintf('%d', $filesize));
header('Expires: 0');

// check for IE only headers
if (isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) {
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
} else {
  header('Pragma: no-cache');
}

$handle = fopen($filepath, 'rb');
fpassthru($handle);
fclose($handle);
Community
  • 1
  • 1
sjvargason
  • 15
  • 5

1 Answers1

2

there is an error in your code. Use

$mime = array('application/octet-stream');
header('Content-Type: '.$mime[0]);

or

header('Content-Type: application/octet-stream');
Markus Madeja
  • 848
  • 7
  • 10
  • I changed the content-type per your suggestion, however it made no difference in the error I'm seeing once the cache is cleared. – sjvargason Aug 06 '13 at 14:19
  • Maybe the problem is Apache is trying to compress the output, so add: @apache_setenv('no-gzip', 1); @ini_set('zlib.output_compression', 0); – Markus Madeja Aug 06 '13 at 14:43
  • probably a dumb question, but where do i put that? i threw it up top by the commented out ini_sets with and without the @'s but neither of those made a difference either. – sjvargason Aug 06 '13 at 15:06