2

How can i collect all the html content of a php page and save it into a doc file and then serve it to user with save prompt.

<?php
 ob_start();
 echo 'Hello World';
 file_put_contents('filename.doc', ob_get_contents());
 header('Content-type: application/msword'); 
 // serve filename.doc to user with a save promt.?????
 ob_end_flush();
 ?>
6
  • 2
    Note that what you are trying to do is not serving a real .doc file, but a HTML file masked as a doc. Results may vary. Other than that, it should be as easy as $data = ob_get_contents(); echo $data; Commented Mar 14, 2011 at 1:05
  • Just writing to a file with suffix .doc, doesn't make it a Word document! This could be interesting: webcheatsheet.com/php/create_word_excel_csv_files_with_php.php Commented Mar 14, 2011 at 1:05
  • People still use .doc format? Commented Mar 14, 2011 at 1:06
  • 2
    @Felix well, it arguably is the only chance of getting a HTML structure into Word somehow without huge hassle Commented Mar 14, 2011 at 1:06
  • @Pekka: Maybe. Honestly, I have not much experience with that. Commented Mar 14, 2011 at 1:09

2 Answers 2

2

First off, it won't be a .doc file. You can't just put any kind of contents in a file, give it a file extension and hope it will work as a .doc file.

Second - you got it almost all right, up to the part where you need to read the file and force browser to download it. To do so, add this to your code (with necessary changes of course):

header('Content-Disposition: attachement;filename="put_filename_here.doc"');
header('Content-Transfer-Encoding: binary');

readfile('/path/to/file.doc');
Sign up to request clarification or add additional context in comments.

1 Comment

thnks it worked.. :-) will accpt ur answer in next 3 mins :-)
0

PHP DOCx is a free library for creating .docx files. http://www.phpdocx.com/

1 Comment

Files generated by this doesnt supports mac microsoft office.. You can try their own provided examples. They will not open in microsoft office 2008 for mac.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.