nazly.me

Weblog of Nazly Ahmed

Nazly Ahmed

Nazly Ahmed

Web Developer. PHP Addict. Wordpress Hacker. FOSS Enthusiast. (Micro)Blogger. Photo Hobbyist. Cricket Fanatic. Husband. Dad.

I had a serious problem today while I was at work. I was working on this project that involved some important pdf documents to be downloaded via https. I wrote the code to read the pdf file which is located outside the webroot and will prompt the download window to the user. This worked fine in Mozilla Firefox which is my primary browser. Later when I checked the script in IE, instead of the filename of the pdf file to be downloaded, it prompted the filename of the PHP script that does the download process.

After breaking my head on this it was Paul (AKA Postalcow @ Codewalkers.com) who rescued me. He mentioned the fact that MSIE does not allow to cache a secure document but it needs to read the document from cache. And also gave this info from the Microsoft website

http://support.microsoft.com/default.aspx?scid=kb;en-us;316431

After that I managed to get it sorted by adding this header to my script

1
2
3
<?php
header("Pragma: cache");
?>

Finally my script looked like this

1
2
3
4
5
6
7
8
9
10
<?php
$fileName="myfile.pdf";
$pathToFile = "/path/to/file/";
$fileName = ereg_replace(".php","",$fileName);
header("Pragma: cache");//This solved the problem
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"$fileName\"");
readfile($pathToFile.$fileName);
exit;
?>
Posted on 30th November 2004 15:32:15