sjvargason
sjvargason

Reputation: 15

PHP download script throws errors in IE8

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);

Upvotes: 0

Views: 356

Answers (1)

Markus Madeja
Markus Madeja

Reputation: 856

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');

Upvotes: 2

Related Questions