title: How can I generate and download a file with one user action in PHP?
body: <p>My PHP app needs to provide my users with a single button that, when clicked:</p>
  
  <ol>
  <li>Executes a function that generates a .csv file and stores it on the server.</li>
  <li>Downloads that file to the user's client.</li>
  <li>Redisplays the same page that contains the button.</li>
  </ol>
  
  <p>The best I have done so far requires two buttons.  FYI, they look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/4LiaY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4LiaY.png" alt="enter image description here"></a></p>
  
  <p>The "Generate Export File" button issues a POST that executes the file generator function.  The HTML for this is:</p>
  
  <pre><code>&lt;form enctype="multipart/form-data" action="season.php?#19" method="POST" name="export_meals_form"&gt;
    &lt;input type="hidden" name="export_meals" value="docs/meals.csv"&gt;
    &lt;p&gt;&lt;input type="submit" value="Generate Export File" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The "Download Export File" button is actually a link.  The HTML for it is:</p>
  
  <pre><code>&lt;p&gt;&lt;a href="docs/meals.csv" download&gt;&lt;button type="button"&gt;Download Export File&lt;/button&gt;&lt;/a&gt;&lt;/p&gt;
  </code></pre>
  
  <p>If the user pushes these two buttons in the right order, everything works fine.  But there's no reason why the user should have to do two clicks (and possibly get it wrong); the two actions are always supposed to happen together and in that order.</p>
  
  <p>I think the problem is that I don't know how to initiate a download from PHP code.  The only way I've found to do a download is for the user to click on a link with the "download" modifier, as above.</p>
  
  <p>Several others have asked this question, and the usual answer sees to be to use the PHP readfile() function, embedded in code like this (from <a href="https://stackoverflow.com/questions/20367104/how-to-download-file-from-database-folder-using-php">How to download file from database/folder using php</a>):</p>
  
  <pre><code>    header('Content-Description: File Transfer');
      header('Content-Type: application/force-download');
      header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
      header('Content-Transfer-Encoding: binary');
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      header('Content-Length: ' . filesize($name));
      ob_clean();
      flush();
      readfile("your_file_path/".$name); //showing the path to the server where the file is to be download
      exit;
  </code></pre>
  
  <p>When I invoke this kind of code from a PHP function, I get an error saying that the headers can't be loaded because the  has already been sent.  If I put the code in the , it simply displays the file contents in my browser and doesn't display the page.</p>
  
  <p>I think there's something basic here that I don't understand.</p>