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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP – 将CSV导出到FTP而不是在浏览器中下载脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经尝试了几个小时现在无济于事.我已使用以下命令在浏览器中成功输出我的CSV作为下载:

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

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

// 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 &amp; 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);

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

脚本宝典总结

以上是脚本宝典为你收集整理的PHP – 将CSV导出到FTP而不是在浏览器中下载全部内容,希望文章能够帮你解决PHP – 将CSV导出到FTP而不是在浏览器中下载所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。