简体   繁体   English

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

[英]export mysql data to csv download file in localhost but not in server

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. 我已经使用将mysql数据下载到CSV的代码,在localhost中工作正常。当我单击导出按钮时,它以CSV格式下载了localhost的文件,但是当我在服务器上运行此代码时以及单击导出按钮时打印没有下载文件的数据。

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

 ?>

Chances of errors: 错误的可能性:

  • Your config may be different on the server. 您的配置在服务器上可能有所不同。
  • <?php and header() should be the first calls within the page. <?phpheader()应该是页面内的第一个调用。

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. 出于调试目的,请禁用header()调用,并在屏幕上查看输出-如果其中包含错误。

Only if it works correctly, set the headers correctly to force a download. 仅当其正常工作时,才能正确设置标题以强制下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM