Reputation: 5805
I'm creating a doc file with the php.
I'm using echo
to output everything but I can see that my html is properly created in my response but the download is not starting.
How can I make this download to start?
Part of my code:
header('Content-Description: File Transfer');
header("Content-type: application/msword; charset=utf-8");
header("Content-Disposition: attachment;Filename=test.doc");
echo $main_html; //html code
Upvotes: 1
Views: 2143
Reputation: 4607
I tested this, and it works perfect.
file_put_contents("test.doc",$main_html);
header('Content-Description: File Transfer');
header("Content-type: application/msword; charset=utf-8");
header("Content-Disposition: attachment;Filename=test.doc");
readfile("test.doc");
unlink("test.doc");
die;
Note : if you may already know, header only works before passing any output to the browser, including line break and even a space .
Upvotes: 1
Reputation: 15629
To start an download, you have to use the content-type application/octet-stream
to start an download as mentioned in RFC 2046.
Btw. you have to echo the content of the doc file and not your html content(i.e. use readfile('test.doc')
as mentioned in other answers or simply echo the content of your doc file)
Upvotes: 0
Reputation: 879
Your code works fine for me.. I ran this on my server
<?php
$main_html = "<h1>Hey I'm Nate</h1>"; // I did put this in
header('Content-Description: File Transfer');
header("Content-type: application/msword; charset=utf-8");
header("Content-Disposition: attachment;Filename=test.doc");
echo $main_html; //html code
Did you define $main_html
before echoing? Because all I changed was added some html to it.
It started a download right away, with the correct content in the document.
Upvotes: 0
Reputation: 1005
Use this:
header("Content-Type: text/html");
header("Content-Disposition: attachment; filename=read.html;");
header("Content-Transfer-Encoding: binary");
echo $html;
Upvotes: 0