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);

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

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备