title: PHP checking if csv file exists?
body: <p>The problem with this code is that each time I export from MySQL to csv  it overwrites the already created csv. I want this code to check if there is an already created csv file -> create new one. Ex. (Sales-2012-9-8.csv) to (Sales1-2012-9-8.csv)...This code works great on exporting records from MySQL..But when it comes to export another csv it overwrites the current csv file name.</p>
  
  <p>Oh yea, one more thing..How is it possible to add Table Headers at the first row of csv file. Ex ( Item No - Qty - Selling Price - Date )?</p>
  
  <p>Thank You</p>
  
  <pre><code>&lt;?php
  require_once('connect_db.php');
  $fdate = $_POST['fdate'];
  $tdate = $_POST['tdate'];
  
  $result = mysql_query("SELECT item_no, qty, discount_price, date FROM sold_items WHERE date BETWEEN '".$fdate."' AND '".$tdate."' ORDER BY date Desc");
  
  $filename = $name = "Sales";
  $index = 1;
  while(file_exists($filename)) {
    $filename = $name.$index;
    $index++;
    echo "File Already Exists";
  }
  
  if(!$f = fopen("E:/{$filename}-{$tdate}.csv", 'w')){
      ?&gt;
      &lt;script language="javascript"&gt;
      alert('USB FLASH NOT INSERTED');
      history.back();
      &lt;/script&gt;
      &lt;?php
  }
  
  while($row = mysql_fetch_array($result, MYSQL_NUM))
  {
      fputcsv($f, $row);
  }
  
  fclose($f);
  
  
  ?&gt;
  &lt;center&gt;&lt;font style="font-size:28px; font-weight:bold; color:#C00;"&gt;USB Transfer Successful&lt;/font&gt;&lt;/center&gt; 
  &lt;center&gt;&lt;input type="button" onclick="javascript:window.close()" value="Close Window" /&gt;&lt;/center&gt;
  &lt;?php
  
  ?&gt;
  </code></pre>