title: readfile downloads an empty file and wrong file type
body: <p>I am using the code below to download a csv file from a button click.</p>
  
  <p><strong>JS</strong>:</p>
  
  <pre><code>$(document).on("click", "#CSV", function () {
  
      var csv_value = $('#result').table2CSV({
          delivery: 'value'
      });
      console.log(csv_value);
      $.ajax({
          type: "POST",
          url: "csv.php",
          data: {
              csv: csv_value
          },
          success: function (html) {
               window.location.href = "download.php";
          }
      });
  
  });
  </code></pre>
  
  <p><strong>csv.php</strong>:</p>
  
  <pre><code>require_once ("session_start.php");
  if (isset ( $_POST ['csv'] )) {
  $file = $_POST['csv'];
  $_SESSION ['csvf'] = $file; 
  } else {
      echo "No CSV Data";
  }
  </code></pre>
  
  <p><strong>download.php:</strong></p>
  
  <pre><code>session_start();
      $file =$_SESSION['csvf'];
      $filename = $file."_".date("Y-m-d_H-i",time());
      header ( "Content-type: application/vnd.ms-excel" );
      header ( "Content-disposition: filename=" . $filename . ".csv" );
      readfile($filename);
      exit ();
  </code></pre>
  
  <p>When I execute the function, the file downloaded is <code>download.php</code> despite the correct headers being passed (confirmed via console), the file is also empty. Also when I use <code>print()</code> it prints the <code>CSV</code> data so I know the file isn't empty but it also generates a <code>.php</code> file and prints all the contents inside including the headers. How do I fix this issue?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I tried brute forcing the download using the following code in <code>.htaccess</code> to no avail.</p>
  
  <pre><code>&lt;FilesMatch "\.(?i:csv)$"&gt;
    ForceType application/octet-stream
    Header set Content-Disposition attachment
  &lt;/FilesMatch&gt;
  </code></pre>
  
  <p>The result is still a <code>download.php</code> file.</p>