5
I’d like to know how to force the download of a file, with some extension accepted by Microsoft Word, through function header.
I searched a few times, tested the MIME types I found, like the ones found in this microsoft list, but I was unsuccessful.
This code works to force the download of a file with extension .txt, but if you try to .doc, an error will occur while trying to open, by word, the downloaded file.
header('Content-Type: application/octet-stream; charset=utf-8');
header('Content-disposition: attachment; filename="' . uniqid(time()) . '.txt";');
header('Content-Length: '. filesize($filename));
readfile($filename);
Full example:
<?php
$file = tmpfile();
$content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent a vehicula mi, eu lacinia sem. Donec pellentesque egestas pulvinar. Donec ultricies risus vitae tellus tincidunt sagittis sed a odio. Curabitur vitae egestas metus, sit amet consectetur nisi. In pellentesque, mauris consequat ornare ullamcorper, mi nunc condimentum leo, ac feugiat lectus metus in nulla. Nulla ac vestibulum lacus. Donec sit amet felis pulvinar, vestibulum metus at, mattis lorem. Duis efficitur, velit vitae ullamcorper blandit, erat quam vehicula risus, dignissim volutpat nulla orci quis augue. Donec nisi velit, sagittis sed consectetur vitae, ornare eget turpis. Vivamus tincidunt, ligula sit amet aliquet iaculis, sapien risus rutrum purus, a sollicitudin nunc lacus sit amet purus. Pellentesque lacinia, tortor ut rhoncus molestie, eros nisl venenatis turpis, eget elementum quam ligula eget eros. Phasellus aliquam neque blandit scelerisque mattis. Maecenas purus erat, sodales at tincidunt eu, facilisis non velit. Quisque blandit arcu non dolor efficitur hendrerit.
Morbi ultricies arcu tempor, convallis sem non, faucibus risus. Praesent ut nunc sit amet quam placerat iaculis non at nibh. Maecenas imperdiet aliquam risus, ut sodales justo consequat id. Donec sollicitudin maximus cursus. Sed placerat, mauris a ornare hendrerit, ipsum enim ornare augue, non efficitur magna nulla auctor mi. Praesent sodales sed erat non luctus. Aliquam erat volutpat. Duis eget semper augue. Suspendisse pellentesque condimentum vehicula. Aliquam tempor arcu non leo vehicula, vitae ornare tortor sagittis. Vivamus posuere nibh massa, et dapibus nisl viverra quis. Phasellus tincidunt tellus id vehicula varius. Cras efficitur libero vitae accumsan tincidunt.
Nam molestie enim non nulla imperdiet interdum. Ut eget tortor venenatis, lobortis est a, consequat leo. Quisque consequat pellentesque velit eget vulputate. Integer pharetra felis sed hendrerit tristique. Donec tempus gravida diam, non maximus metus semper ac. Nulla ultrices egestas turpis. Fusce at dui ligula. Nam eget ligula sit amet quam tincidunt mollis. Mauris vitae dapibus lectus. Vestibulum id purus lacus. Proin tincidunt felis ac suscipit tristique.
Maecenas ullamcorper quis risus nec suscipit. Nam sodales tincidunt laoreet. Mauris interdum auctor massa, sit amet blandit nulla ullamcorper hendrerit. Curabitur ante libero, feugiat id iaculis quis, vestibulum ut eros. Morbi eleifend leo vitae mi laoreet eleifend. Ut vel accumsan mi. Cras sit amet bibendum tortor, nec vestibulum risus. Vestibulum suscipit rutrum turpis eu lacinia. Mauris porttitor maximus eros. Nullam augue arcu, sodales et porttitor eget, hendrerit eu odio. Vestibulum suscipit nisi sed commodo posuere. Proin volutpat nunc non neque accumsan posuere. Duis consequat, leo nec dictum sodales, neque leo congue nisl, eget consequat odio dolor eget nunc. Proin accumsan enim nisi.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nullam egestas hendrerit nisi, tristique pretium velit condimentum vel. Etiam sit amet purus nec risus finibus finibus auctor in metus. Integer nec risus mauris. Fusce aliquet egestas purus sed venenatis. Fusce non finibus sapien. Integer eu augue ligula. Sed urna sapien, hendrerit et pharetra sed, posuere eu elit. Sed a nibh id ipsum scelerisque iaculis. Vestibulum convallis sollicitudin erat ut blandit. Integer scelerisque odio vel nisl ullamcorper, vel feugiat nibh pretium. Sed et lectus vulputate, feugiat lacus vitae, lobortis sem.";
fwrite($file, $content);
fseek($file, 0);
$metadata = stream_get_meta_data($file);
$filename = $metadata["uri"];
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-disposition: attachment; filename="'. uniqid(time()) . '.docx";');
header('Content-Length: '. filesize($filename));
readfile($filename);
I don’t know if I understand... You want to turn an aquivo
.php
in.doc
and then make it available for download? If so has the Phpword...– gustavox
@gustavox No, I want to take any text, it can be coming from the database or not, and export it in . doc
– Thomerson Roncally
So explain how you want to do this. Specific function in PHP I think does not exist, I’m almost sure you will need Phpword even... take a look.
– gustavox
With Phpword, you can create DOCX, ODT, or RTF Documents dynamically using your PHP 5.3+ scripts. Below are some of the Things that you can do with Phpword library: [...] Create header and footer for each sections
– gustavox
@Okay, I’ll take a look at this class, but "I heard" that could force the download the same way q a txt file, I don’t know...
– Thomerson Roncally
I wish there was, I could use it... :-)
– gustavox
@gustavox rsrsrs as it is... I’ll leave it open here, see if more suggestions arise
– Thomerson Roncally
nah, forget everything I said before... see that.
– gustavox
@Thomersonroncally Always add a functional example of the problem, so it makes it easier for people to detect the fault, I recommend reading: http://answall.com/help/mcve, I removed the "if you need the comment code" part because always enter the code, it does not need to be complete, it only needs to "work" and needs to reproduce the problem (it must be Minimal, Complete and Verifiable) --- I hope the answer helps you :) See more.
– Guilherme Nascimento
@Gustavox strange that your second comment says the answer (you will need the same Phpword), now that I saw, I did not understand why not added it the answer. The example of the author will not work precisely for this, he is trying to send a simple text pro output and force but with the extension
.doc
, However software like Msword and Libreoffice cannot read this. The docx file for example 'and a zip file containing several files, among them the main and an xml (office open xml https://en.wikipedia.org/wiki/Office_Open_XML_file_formats) :)– Guilherme Nascimento
Yes @Guilhermenascimento, I had already looked at this library, it’s very good, but at the time I found it a little complicated for me... and as ap said it was possible, I thought it would be much simpler to use only the header, since a part of what I need is only text (Phpword will be better if I get it because it rents all html right.)... But anyway, so there is no way anyway, because in Open Office it opens, but it is opening in asc II right, and the windows word does not open like this... I downloaded. Thanks so far! Hugs. + 1
– gustavox
In time: I was able to install right, and already I get the examples that come in the folder, but I still could not make work with the code generated dynamically from my form, but I think this would be another question... Thanks.
– gustavox
Thanks @gustavox :)
– Guilherme Nascimento