Okay, I've seen lots of threads on this, and have been struggling with it myself. How to do make the browser download a file?
I've been building a courseware system that has to let students upload their papers and whatnot - and let instructors download them, add comments, and upload them back to the student.
I'm storing the files on the server, but outside of the web directory - that way I can more easily control who gets what file through a download script.
Its this download script that has been the source of a much consternation. After playing with the headers and trying different solutions posted by people, I've come up a with a solution that works for us on all the browsers I could test it on.
( IE 5.0, 5.5, 6.0 - Netscape 4.0,4.7 )
Send the file
header("Content-type: $mimeType");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Description: Laika Attachment");
fpassthru(fopen("/public/attachments/$filename","r"));
( The $mimeType should be the actual mimeType of the file you're sending. Not application/octet-stream, or x-msdownlaod or anything like that.)
Just these three headers, no more no less. Other headers, like the no-cache pragma, filesize, or setting the type to binary, caused weird problems on one or more of the browsers.
Another important thing I found, was you have to use the filesystem functions to stream the file through to the browser. There were a couple of suggestions that used include ("/public/attachments/$filename");
instead of the filesystem fuinctions. It was usually a toss up whether that would work or not, depending on the type of file that was being downloaded. Many times, the server would actually try to parse the file, rather than just sending it to the browser.