user399666
user399666

Reputation: 19909

Forcing download of txt file via PHP is including HTML

Basically, the user will be exporting some data to a txt file. That txt file will then download automatically. The download is working fine and is showing the data I want it to. However, the HTML of the actual page is getting appended onto the end of it?

$fh = fopen($filename, 'w');
fwrite($fh, $data); 
fclose($fh);

header('Content-disposition: attachment; filename='.$filename);
header('Content-type:  text/plain');
readfile($filename);

Upvotes: 0

Views: 2037

Answers (1)

Tim
Tim

Reputation: 6441

If there is html after the code above, you may want to exit after readfile($filename)?

Upvotes: 3

Related Questions