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

将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(); 
}

 ?>

图片转代码服务由CSDN问答提供 功能建议

我在php中使用了下载mysql数据到csv的代码。在localhost中工作正常。当我点击 导出按钮它以csv格式在localhost下载文件,但是当我在服务器上运行此代码时,当我单击导出按钮时,它会打印它没有下载文件的数据。

 &lt; form method =“post”&gt; 
&lt; input type =“submit”name =“export”value =“export”&gt; 
&lt; / form&gt  ; 
&lt;?php 
require'db.php'; 
if(isset($ _ POST ['export'])){
 $ q = mysql_query(“select firstname,lastname,来自tab_Recruiter的电子邮件,其中status = 1”  ); 
 
header('Content-Type:text / csv; charset = utf-8'); 
header('Content-Disposition:attachment; filename = Userinfo.csv'); 
header(“Pragma:no- 缓存“);  
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();  
} 
 
?&gt; 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题