Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http header for downloading Microsoft Word and Excel files

Tags:

html

php

I can download my microsoft word successfully if I named it in the filename by default. But if I use $variables to name it. The document extension will be unknown.

Sample:

$No = 1; 
$Name = 'John'; 
$Test = 'Science';

//Download header
$document->save($doc);
header('Content-Description: File Transfer');
header('Content-Type: application/msword');
header("Content-Disposition: attachment; filename='$No_$Name_$Test.docx");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($doc));
ob_clean();
flush();
readfile($doc);

So if i rename my filename as variables. The file download will be without the docx extension. Anyone can advise?

Thanks

like image 822
JLearner Avatar asked Jul 14 '12 21:07

JLearner


People also ask

How do I import a header from Excel to Word?

One way to copy headers and footers from a worksheet in one workbook to a worksheet in another is to use the traditional editing methods of copying and pasting. In other words, you can select the header material you want to copy, press Ctrl+C, display the header in the target worksheet, and then press Ctrl+V.


1 Answers

Correct headers are

for Excel (*.xlsx):

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $fileName . '"');

for Word (*.docx):

header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="' . $fileName . '"');
like image 195
Konstantin Smolyanin Avatar answered Sep 21 '22 19:09

Konstantin Smolyanin