title: Generate CSV file on an external FTP server in PHP
body: <p>I have some PHP code that successfully exports a MySQL table to a CSV file. </p>
  
  <p>I would like to add to that code, so instead of saving locally the CSL file is exported to/saved on an external FTP server.</p>
  
  <p>My current code:</p>
  
  <pre><code>//open database connection                            
  require ('../database-config.php');
  
  //name the file
  header('Content-Type: text/csv');
  header('Content-Disposition: attachment;filename=exported-data.csv');
  
  //SQL Query for Data
  $sql = "SELECT * FROM data;";        
  //Prepare Query, Bind Parameters, Excute Query
  $STH = $dbh-&gt;prepare($sql);
  $STH-&gt;execute();
  
  //Export to .CSV
  $fp = fopen('php://output', 'w');
  
  $first_row = $STH-&gt;fetch(PDO::FETCH_ASSOC);
  $headers = array_keys($first_row);
  
  fputcsv($fp, $headers); // put the headers
  fputcsv($fp, array_values($first_row)); // put the first row
  
  while ($row = $STH-&gt;fetch(PDO::FETCH_NUM))  {
  fputcsv($fp,$row); // push the rest
  }
  fclose($fp);
  </code></pre>
  
  <p>I know I'll need to add some new variables;</p>
  
  <pre><code>$ftp_server="ftp.remotersite.com";
  $ftp_path="/path/to/somefile";
  $ftp_username="username";
  $ftp_userpass="password";
  </code></pre>
  
  <p>But, I'm not sure the best way to use <code>ftp_put</code> (or should it be <code>ftp_fput</code>?) to transfer to the external destination.</p>
  
  <p>Thanks in advance.</p>