简体   繁体   中英

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.

<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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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