dongyan1993 2016-10-08 11:16
浏览 73
已采纳

将mysql数据导出到localhost中的csv下载文件,但不在服务器中

I have used the code of download mysql data to csv in php.It works fine in localhost.when i click on export button it download the file in localhost in csv format but when i run this code on server and when i click export button it print the data it did not download the file.

<form method="post">
<input type="submit" name="export" value="export">
</form>
<?php 
require 'db.php';
if(isset($_POST['export'])){
 $q= mysql_query("select firstname,lastname,email from  tab_Recruiter where status=1");

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=Userinfo.csv');
header("Pragma: no-cache"); 
header("Expires: 0");

$data = fopen('php://output', 'w');
$first = true;
 while($row = array_filter(mysql_fetch_assoc($q))){
 if ($first) {
        fputcsv($data, array_keys($row));
        $first = false;
    }
   // fputcsv($fp, $row);
fputcsv($data, $row);
}
exit(); 
}

 ?>
  • 写回答

1条回答 默认 最新

  • dongsi3826 2016-10-08 16:49
    关注

    Chances of errors:

    • Your config may be different on the server.
    • <?php and header() should be the first calls within the page.

    Also, do not DOWNLOAD immediately with headers. For debugging purpose, disable your header() calls and see the output in the screen - if it contains errors.

    Only if it works correctly, set the headers correctly to force a download.

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

报告相同问题?

悬赏问题

  • ¥15 MPLS/VPN实验中MPLS的配置问题
  • ¥15 materialstudio氢键计算问题
  • ¥15 已知隐函数其中一个变量的,求另外一个变量
  • ¥15 echarts图表制作
  • ¥15 halcon根据玻璃面板纹路取区域
  • ¥15 HFSS设计小型化180度耦合器
  • ¥15 使用CInternetSession,CHttpFile读取网页文件时有些电脑上会卡住怎么办?
  • ¥15 水下机器人的半物理仿真研究
  • ¥15 微服务假死,一段时间后自动恢复,如何排查处理
  • ¥50 webrtc-streamer TCP rtsp