title: How i can create a file on-the-fly in php and download it
body: <p>I have the following php script which checks the submitted value of the check-box elements and prints their values if those were checked. </p>
  
  <pre><code>&lt;?php
  echo '&lt;table class="features-table"&gt;';
  
  echo "&lt;tbody&gt;";
  for ($i=1;$i&lt;=2284;$i+=1) {
    if($_POST[$i]) {
      echo"&lt;tr&gt;";
              echo "&lt;td&gt;&lt;a href=http://www.m-w.com/dictionary/" . $_POST[$i] . "&gt;" . $_POST[$i].  "&lt;/a&gt;&lt;/td&gt;";
      echo "&lt;/tr&gt;";
    }
  }
  
  ?&gt;
  </code></pre>
  
  <p>I want to make this data shown to the user, available for download as text file. How should I create the download button. Do I need separate php script in that case how will I get the form data submitted to this php script?</p>
  
  <p>From google search I got that I need to use code similar to below</p>
  
  <pre><code>header("Content-type: text/plain");
  header("Content-Disposition: attachment; filename=list.txt");
  header("Pragma: no-cache");
  header("Expires: 0");
  echo 'data1,data2,data3...';
  </code></pre>
  
  <p>But I am not getting how do I integrate it with current php script.</p>
  
  <p>Thanks</p>