Getting a database export sent by ftp to another server
<p>OK here is what I am trying to do</p>

<p>Export data from the database, write a csv, then publish to another server.</p>

<p>It will output a csv and download, but I want to write the file on the server, and then publish it from my server to another one.</p>

<p>here goes...</p>

<pre><code>function exportMysqlToCsv($table,$filename = 'cmv_sailings.csv')
{
$csv_terminated = "\n";
$csv_separator = ",";
$csv_enclosed = '"';
$csv_escaped = "\\";
$sql_query = "select * from $table";

// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);

$schema_insert = '';

for ($i = 0; $i &lt; $fields_cnt; $i++)
{
    $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
        stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
    $schema_insert .= $l;
    $schema_insert .= $csv_separator;
} // end for

$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;

// Format the data
while ($row = mysql_fetch_array($result))
{
    $schema_insert = '';
    for ($j = 0; $j &lt; $fields_cnt; $j++)
    {
        if ($row[$j] == '0' || $row[$j] != '')
        {

            if ($csv_enclosed == '')
            {
                $schema_insert .= $row[$j];
            } else
            {
                $schema_insert .= $csv_enclosed .
                str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
            }
        } else
        {
            $schema_insert .= '';
        }

        if ($j &lt; $fields_cnt - 1)
        {
            $schema_insert .= $csv_separator;
        }
    } // end for

    $out .= $schema_insert;
    $out .= $csv_terminated;
} // end while

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
}
</code></pre>

<p>// FTP part</p>

<pre><code>$connection = ftp_connect("ftp server");
$username = "some user";
$password = "some password";
$login = ftp_login($connection, $username, $password);

if (!$connection) {

    die('oops, connection failed');

} else {

    echo "connection ok &lt;br /&gt;";

}

// Ok, I'm connected, log me in...

if(!$login) {

    die('login failed');

} else {

    echo "logged in ok &lt;br /&gt;";

}

// Set passive FTP mode

ftp_pasv($connection, true);

// Upload the file

$upload = ftp_put($connection, $filename, $filename, FTP_ASCII);

if(!$upload) { echo "upload failed, doh!"; } else { echo "upload done, thanks!"; }

// We're done, close the connection

ftp_close($connection);
</code></pre>

<p>Does anyone know what I'm missing here ?</p>

<p>thank</p>

<p>Rich :)</p>
