0

I'm trying to fetch and download data using php to csv file, for task i found a good answer, this is working for me on all browser except firefox.

$s = $master->getUser();
function array2csv(array &$array)
{
   if (count($array) == 0) {
     return null;
   }
   ob_start();
   $df = fopen("php://output", 'w');
   fputcsv($df, array_keys(reset($array)));
   foreach ($array as $row) {
      fputcsv($df, $row);
   }
   fclose($df);
   return ob_get_clean();
}

function download_send_headers($filename) {

    /*header("Content-Type: application/csv");
    header("Content-Disposition: attachment; filename={$filename}");

    // Disable caching
    header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
    header("Pragma: no-cache"); // HTTP 1.0
    header("Expires: 0"); // Proxies*/

     // disable caching
    $now = gmdate("D, d M Y H:i:s");
    header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
    header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
    header("Last-Modified: {$now} GMT");

    // force download  
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");

    // disposition / encoding on response body
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");
    header("Content-Disposition: attachment;filename={$filename}");
    header("Content-Transfer-Encoding: binary");
}
download_send_headers(site_title .' '. date("d M Y") . ".csv");
echo array2csv($s);
die();

In Firefox file extension does not showing in csv, it is looking like application file.

enter image description here

Community
  • 1
  • 1
ayaz khan
  • 141
  • 8

1 Answers1

0

Have you tried removing these in your code:

header("Content-Type: application/force-download");
header("Content-Type: application/download");

Just retain:

header("Content-Type: application/octet-stream");
James A
  • 775
  • 5
  • 11