Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Create and Download Doc File

So I'm trying to both dynamically create a .doc file and have the user download it when he clicks a button.

These are the headers i found to download a file

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');

And these are the headers i found to make a a word document

header('Content-type: application/vnd.ms-word');
header('Content-Disposition: attachment; Filename='.$myFile);

I'm just having a hard time just fitting the picture together because they both tasks have a 'Content-Type' header. Do i create the file first, save it, then download it? Or can i do it all (create a doc file and have user download it) in one php file?

like image 909
blee908 Avatar asked Oct 31 '11 22:10

blee908


2 Answers

You only need the "headers found to make a word document." The first set are for a generic streaming download.

like image 162
Niet the Dark Absol Avatar answered Oct 15 '22 19:10

Niet the Dark Absol


Your second set of headers are fine. No need for the first. The Content-Disposition header is the one that will typically force a download. (Although, you should be aware that clients can do whatever they want with a file, and you have no direct control over this.)

You can create the file and send it straight to the client without saving it to the server's disk, depending on how you are creating this document.

like image 22
Brad Avatar answered Oct 15 '22 19:10

Brad