dsij89625 2013-06-13 11:24
浏览 60
已采纳

使用php将mysql查询保存为远程ftp服务器上的csv文件

I am trying to export a MySQL query result to a remote ftp server.

I have the below code but I am currently getting an error:

Warning: ftp_put() [function.ftp-put]: Opening ASCII mode data connection. in /home/hulamyxr/public_html/kisv2/xmltest/export.php on line 50

I would think that my $file = $csv_filename; might be the issue as this is fetching the csv file that has just been created on my local server?

any ideas?

My syntax is:

<?php
$host = 'localhost';
$user = 'un';
$pass = 'pwd';
$db = 'dbname';
$table = 'v2ReportingTable';
$file = 'export';
$datetime=date("Y-m-d H:i:s");
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());

//Create a CSV for 
$result = mysql_query("SELECT * FROM dbname.v2ReportingTable"); 
if (!$result) die('Couldn\'t fetch records'); 
$num_fields = mysql_num_fields($result); 
$headers = array(); 
for ($i = 0; $i < $num_fields; $i++) 
{     
       $headers[] = mysql_field_name($result , $i); 
} 
$csv_filename = "export-" .$datetime.".csv";
$fp = fopen($csv_filename, 'w+');


if ($fp && $result) 
{
    fputcsv($fp, $headers);
    while ($row = mysql_fetch_row($result)) 
    {
        fputcsv($fp, array_values($row)); 
    }
}

//works till here

$ftp_server = "ftp.server.co.za";
$ftp_user_name = "un";
$ftp_user_pass = "pw";
$file = $csv_filename;
$remote_file = "/LocExports/".$file;


// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
 echo "successfully uploaded $file
";
} else {
 echo "There was a problem while uploading $file
";
}

// close the connection
ftp_close($conn_id);
?>

Thanks Again

  • 写回答

1条回答 默认 最新

  • dongyange1101 2013-06-13 14:34
    关注

    and it creates the file in the correct location but is a 0kb file and all FTP commands thereafter fail. It is likely that the client is behind a firewall. To rectify this use:

    <?php
    

    ftp_pasv($resource, true);

    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器