title: php export to csv user/browser download
body: <p>After much googling and tinkering around a bit i managed to export the table to .csv file but now i'd like to initiate the download via the browser instead of automatically storing in a folder.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>&lt;?php
  $server = 'localhost';
  $user = 'root';
  $pass = 'pass';
  $db = 'test';
  
   $db = mysqli_connect($server, $user, $pass, $db);
  
  mysqli_set_charset($db, "utf8");
  
  $filename = 'uploads/'.strtotime('now').'.csv';
  
  $fp = fopen($filename,"w");
  
  $query = "SELECT id,product,status,created,summary FROM product_table";
  
  $result =  mysqli_query($db,$query) or die( "My query ($query) generated an error: ".mysql_error());
  
  $row = mysqli_fetch_assoc($result);
  
  $seperator = "";
  $comma = "";
  
  foreach ($row as $name =&gt; $value){
      $seperator.= $comma. ''.str_replace('','""',$name);
      $comma=",";
  
  }
  $seperator .= "\n";
  
  echo $seperator;
  
  //putting the heading into the csv file
  fputs($fp,$seperator);
  
  
  mysqli_data_seek($result,0);
  
  while ($row = mysqli_fetch_assoc($result)){
  
  
  $seperator = "";
  $comma = "";
  
  foreach ($row as $name =&gt; $value){
  
      if (strcmp($name,'summary') == 0){
  
  
              $value = str_replace( array( "\r" , "\n", "\r\n", "\n\r" ) ,'' , $value);
              $value = str_replace('&lt;/b&gt;&lt;br&gt;','',$value);
              $value = str_replace('&lt;b&gt;','',$value);
              $value = str_replace('&lt;br&gt;','',$value);
              $value = str_replace('&lt;br /&gt;','',$value);
      }
  
      $seperator.= $comma. ''.str_replace('','""',$value);
      $comma=",";
  
      }
  $seperator .= "\n";
  
  
  //putting the heading into the csv file
  fputs($fp,$seperator);
  
  
  }
  
  fclose($fp);
  
  
  
  ?&gt;
  </code></pre>
  
  <p>Running the above script stores the .csv file in the folder uploads. What changes to do i need to make to enable browser download so that users can run the script and download the .csv file via the browser? thanks!</p>
  
  <p><strong>EDIT:</strong>
  Furthermore what modifications would i have to make if wanted to disable saving to the folder and only enable user/browser downloads? thanks.</p>