scriptygoddess

09 Feb, 2007

Force File Download

Posted by: Jennifer In: Bookmarks|PHP Related|Script snippet|Scripts

How to force a file to download (without having to zip it up).

$filename = "secure/writeToFile.doc";

header("Content-Length: " . filesize($filename));
header('Content-Type: application/msword');
header('Content-Disposition: attachment; filename=writeToFile.doc');

readfile($filename);

a few other content types – lots more at the above link…

"pdf": "application/pdf"
"zip": "application/zip"
"xls": "application/vnd.ms-excel"
"ppt": "application/vnd.ms-powerpoint"
"gif": "image/gif"
"png": "image/png"
"jpg": "image/jpg"
"mp3": "audio/mpeg"
"mp3": "audio/mp3"
"wav": "audio/x-wav"
"mpe": "video/mpeg"
"mov": "video/quicktime"
"avi": "video/x-msvideo"

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

2 Responses to "Force File Download"

1 | Michelle

February 13th, 2007 at 2:40 pm

Avatar

Well this is handy. I just used this in a script that creates .csv files to import into a program for our church.

2 | xgrapher

April 17th, 2007 at 12:55 pm

Avatar

It's Ok but it fails after downloading about 20secs or files crossing 2M – I guess failure occurs when 2 files are downloading simulteanously.

Comment Form

Featured Sponsors


  • Curt: If anyone comes across this with similar issues I was able to sort out the pagination issues painlessly with easyCommentsPaginate from http://www.mush
  • Christopher: Yeah, it is indeed hard to do. And something remains elusive about why the pagination never worked. I tried everything I could find. Regardless, I
  • Jennifer: Hi Christopher, always hard to bug test stuff like that remotely. Sorry those didn't help. Glad you found a solution though :)

About


Advertisements