dongyan1993 2016-10-08 03: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 08: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 VAE代码如何画混淆矩阵
  • ¥15 求遗传算法GAMS代码
  • ¥15 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国