views:

562

answers:

4

I have a script that takes a key from $_GET['key'] , looks up the location in a database and uses the readfile together with some headers to present a download for the use. This works in Firefox but not IE8, haven't been able to test it on another IE. I get the following error in IE: "Internet Explorer cannot download download.php from www.example.com". As if it is trying to download the PHP script.


$the_query = "SELECT * FROM `files` WHERE `user_id`=" . $_SESSION['user_id'] . " AND `key`='" . $key . "'";

$result = mysql_query($the_query);
$row = mysql_fetch_array($result);

$file = '/var/www/vhosts/www.example.com/httpsdocs/uploads/' . $row['id'] . '/' . $row['file'];

header("Content-type: application/octet-stream");
header("Content-length: ".filesize($file));
header('Content-Description: File Transfer');
header("Cache-control: private");
header('Content-Disposition: attachment; filename=' . rawurlencode(basename($file)));
readfile($file);
+2  A: 

Replace this:
header("Content-type: application/octet-stream");
with this:
header("Content-Type: application/force-download");

According to this post, IE doesn't normally listen to your headers, and instead looks for itself what you are sending.

Chacha102
I already tried this before, same error unfortunately, thanks.
Swanny
`application/octet-stream` is the official MIME media type for data that is intended to be downloaded (see RFC 2046).
Gumbo
I'm wondering how people know all those RFCs?
Time Machine
@Nevermind: http://www.google.com/search?q=rfc%20application/octet-stream
Gumbo
A: 

Managed to get this working by using the first example from php.net

http://us3.php.net/manual/en/function.readfile.php


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;

Swanny
A: 

Its really a very useful. Its working in IE7 to download the files.

Kapil