加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP – 将CSV导出到FTP而不是在浏览器中下载

发布时间:2020-12-13 22:49:16 所属栏目:PHP教程 来源:网络整理
导读:我已经尝试了几个小时现在无济于事.我已使用以下命令在浏览器中成功输出我的CSV作为下载: header("Content-type: application/csv");header("Content-Disposition: attachment; filename=".$csv_filename.""); 但是我希望输出现在转到FTP服务器,我已经将连
我已经尝试了几个小时现在无济于事.我已使用以下命令在浏览器中成功输出我的CSV作为下载:

header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=".$csv_filename."");

但是我希望输出现在转到FTP服务器,我已经将连接和临时文件部分排序,但似乎无法编辑文件以输出我的结果.

// create empty variable to be filled with export data
$csv_export = '';

for($i = 0; $i < $field; $i++) {
$csv_export.= mysql_field_name($query,$i).',';
}
// newline (seems to work both on Linux & Windows servers)
$csv_export.= '
';

// loop through database query and fill export variable
while($row = mysql_fetch_array($query)) {
// create line with field values
for($i = 0; $i < $field; $i++) {
$csv_export.= '"'.$row[mysql_field_name($query,$i)].'",';
}   
$csv_export.= '
';  
}

上面是获取查询数据并将其放入CSV的部分,我尝试在循环中合并fput和fputcsv以将行写入文件.

//Upload the temporary file to server
ftp_fput($ftpstream,'/httpdocs/.../abandoned.csv',$temp,FTP_ASCII);

$fp = fopen('var/www/vhosts/.../abandoned.csv','w');
fputs($fp,'$csv_export');
fclose($fp);
echo($csv_export);

因此,回显输出工作绝对正常 – 我想要的是将回波数据写入CSV文件并上传到FTP.

我得到的错误是:

Array ( [type] => 2 [message] => fclose() expects parameter 1 to be resource,boolean given

所以fp导致问题……是在开放阶段还是在写csv_export时?

提前致谢.

解决方法

试试这个

//Write the contents to the temp file
fputs($temp,$csv_export);

//upload the temp file
ftp_fput($ftpstream,FTP_ASCII);

//close the temp file
fclose($temp);

您当前的代码是创建一个空文件,上传它,然后创建一个单独的文件.即使你让它在本地服务器上工作,这也是一个不必要的步骤.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读