问题
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);
回答1:
To solve the error : "Internet Explorer cannot download download.php from www.example.com", Add these headers to your script:
header("Pragma: ");
header("Cache-Control: ");
The code will remove the Cache-Control from headers which makes the download problem.
The above code should be added at the top of the file.
It works fine for us.
回答2:
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;
回答3:
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.
回答4:
Just a hint, if someone (like me) is facing problems with directly entering a filedownload into the address bar using a secured https-request. There is a IE bug that is causing this download to fail:
http://support.microsoft.com/kb/323308/en-us
Only workaround seems to be setting the cache-headers according to the article.
回答5:
Never replace this: header("Content-type: application/octet-stream");
with this: header("Content-Type: application/force-download");
"application/octet-stream" is simply the most universal and works on most browsers.
I tried using "application/zip" in one of my tests since I was technically dealing with a ZIP file, but IE6.0 corrupted the download! Everything else behaved normally though. But yeah, had to switch back to "application/octet-stream" so any code out there that tries to detect the file extension, and switch to other content-types specific to the extension are risky! You're better off using "application/octet-stream" for ALL binary files!
来源:https://stackoverflow.com/questions/1218925/php-script-to-download-file-not-working-in-ie