Generating dynamic files for download from PHP. More details at http://sudarmuthu.com/blog/generating-dynamic-files-for-download-from-php
<?php
if (isset($_POST['text']) && $_POST['text'] == 'saveastext') {
// some condition on which we should trigger this
header('Content-Type: text/plain'); // you can change this based on the file type
header('Content-Disposition: attachment; filename="readme.txt"');
// echo the file contents
exit();
}
?>