Skip to content
Advertisement

How to download file as document in Internet Explorer? [closed]

My file download as document is not working in IE. How can I write code in PHP to download the file correctly in Internet Explorer

Advertisement

Answer

The following is the code i have been using for the past three years and it is working fine. Just now i tested in all browsers IE6, IE7, IE8 and in all those it is working fine. The $fn is the full path of the file. and make sure that no other extra spaces or newlines are there anyway i have used ob_clean before flushing the file to the browser.

Better if you have no other html code sent above this… so use it seperately with no other content. else start the code with ob_start(); so at the line of ob_clean() there wont be any extra chars.

$filename = "$fn";
ob_clean();
header("Cache-Control: no-store");
header("Expires: 0");
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment; filename="".basename($filename).""");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);

I have a file management module where i upload files and the user can download it. If you still have issues or unable to make it work the just add few more lines of your problem and the people who ever sees would respond to that.

THE REASON I HAVE USED SOME MORE HEADERS IS TO MAKE SURE THAT IT WORKS BECAUSE INITIALLY I HAD PROBLEMS WITH IE 6.

for secure servers HTTPS replace the first two headers with this one.

header("Cache-Control:  maxage=1");
header("Pragma: public");

this way it is working for me.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement